In android studio I need to deactivate the positive(OK) button of an AlertDialog
until the user types into the EditText
.

- 285
- 3
- 14
3 Answers
Simply create a custom alert dialog, then apply your logic.
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
View mView = layoutInflaterAndroid.inflate(R.layout.your_dialog_xml, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(context);
alertDialogBuilderUserInput.setView(mView);
alertDialogBuilderUserInput.setCancelable(false);
final AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
final TextView tv_title = mView.findViewById(R.id.tv_title);
final EditText et_message = mView.findViewById(R.id.et_message);
final Button bt_ok = mView.findViewById(R.id.bt_ok);
final Button bt_cancel = mView.findViewById(R.id.bt_cancel);
tv_message.setText(message);
bt_ok.setText(ok_text);
bt_cancel.setText(cancel_text);
// set on text change listener and enable buttons as per your choice
et_message.set....
bt_ok.setOnClickListener(view -> {
alertDialogAndroid.cancel();
if (callback != null) callback.onSubmit("");
});
bt_cancel.setOnClickListener(view -> {
alertDialogAndroid.cancel();
if (callback != null) callback.onCancel();
});
alertDialogAndroid.show();

- 3,096
- 1
- 10
- 21
try this code hope it will help you
bt_ok.setenable(false);
and enable it on text change
bt_ok.setenable(true);

- 161
- 2
- 7
-
1Worked with charm :) – Haseeb Javed Dec 11 '20 at 07:04
First of all, you should create a custom alert dialog in your layout folder.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:layout_margin="10dp"
android:textSize="15sp"
android:hint="@string/hint"
android:inputType="textCapWords"
android:singleLine="true"
android:layout_marginTop="10dp"
android:background="@null"
android:maxLines="1"
android:scrollbars="vertical"
android:importantForAutofill="no" />
</LinearLayout>
Then a layout inflater for the edit text given above so that our alert dialog will be able to show and get the text written inside.
LayoutInflater layoutInflater= this.getLayoutInflater();
final View view = layoutInflater.inflate(R.layout.dialog, null);
EditText editText = view.findViewById(R.id.edit_text);
After that, we need to create an alert dialog. What is necessary here is to specify the edit text with setView()
function by passing the final view we created. Since you do not have a cancel button, we do not need to call builder.setCancelable(false)
here.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
builder.setPositiveButton("OK", (dialogInterface, i) -> {
//Whatever you would like to do after pressing the OK button.
});
AlertDialog dialog = builder.create();
dialog.show();
In order to get the OK button, we need to call through the dialog's getButton()
function. Remember you need to call setEnabled(false)
so that it will be disabled by default.
Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setEnabled(false);
The last step is to add a text watcher to the edit text so that we can be informed when anything is typed.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (editText.getText().length() > 0){
button.setEnabled(true);
}else{
button.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
The final code in a function should look like this:
public void getAlertDialog() {
LayoutInflater layoutInflater= this.getLayoutInflater();
final View view = layoutInflater.inflate(R.layout.dialog, null);
EditText editText = view.findViewById(R.id.edit_text);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
builder.setPositiveButton("OK", (dialogInterface, i) -> {
//Whatever you would like to do after pressing the OK button.
});
AlertDialog dialog = builder.create();
dialog.show();
Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setEnabled(false);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (editText.getText().length() > 0){
button.setEnabled(true);
}else{
button.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}

- 184
- 1
- 3
- 12