-2

I am making a counter. On load or onCreat I want to show an input dialog box for user to input an integer value. 1st... If user doesn't enter any character value in box. Then User is not able to click Ok button. 2nd.. If user doesn't enter any value in it. Then he will not able to hit Ok or Cancle button.

Please help....!! Thanks in advance

Here is prompt box code

AlertDialog.Builder ab = new AlertDialog.Builder(TasbihWorkActivity.this);
        ab.setTitle("Enter Any String");
        final EditText editText = new EditText(TasbihWorkActivity.this);
        ab.setView(editText);

        ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                count_num.setText(editText.getText().toString());
            }
        });

        ab.setNegativeButton("Cencel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                count_num.setText("No text entered!!!");
            }
        });

        AlertDialog a = ab.create();
        a.show();

1 Answers1

0

You should use Text Watcher for your editText. If no input, disable these two button using setClickable(false); . If doesn't enter any character value, disable OK button

    AlertDialog.Builder ab = new AlertDialog.Builder(TasbihWorkActivity.this);
            ab.setTitle("Enter Any String");
            final EditText editText = new EditText(TasbihWorkActivity.this);

    editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        // implement your logic here
    }
});
            ab.setView(editText);

            ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    count_num.setText(editText.getText().toString());
                }
            });

            ab.setNegativeButton("Cencel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    count_num.setText("No text entered!!!");
                }
            });

            AlertDialog a = ab.create();
            a.show();

Edit

To prevent dialog disappear when screen clicked, add below code

ab.setCancelable(false)

John Joe
  • 12,412
  • 16
  • 70
  • 135