Eric Low
Eric Low

Reputation: 83

EditText in Alert Dialog Android

I have a method to create an Alert Dialog in a fragment

private void alertDialog() {
final EditText editTextField = new EditText(this.getContext());
AlertDialog dialog = new AlertDialog.Builder()
    .setTitle("Title")
    .setMessage("Message")
    .setView(editTextField)
    .setPositiveButton("OK", this)
    .setNegativeButton("Cancel", null)
    .create();
dialog.show();
}

Then I have implemented the DialogInterface.OnClickListener

public void onClick(DialogInterface dialogInterface, int i){
 switch(i){
    case DialogInterface.BUTTON_POSITIVE:
        String name = String.valueOf(editTextField.getText());
        break;
    case DialogInterface.BUTTON_NEGATIVE:
        break;
   }
 }

However the editTextField in onClick method doesn't able to reference to the Alert Dialog. Is there anyway to solve this? Or the only way is to create a layout with edit text and setView into alert dialog builder?

Upvotes: 3

Views: 16109

Answers (3)

Sadique Khan
Sadique Khan

Reputation: 320

use thi

    EditText editText = new EditText(this);
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("Create group")
            .setMessage("Enter group name")
            .setView(editText)
            .setPositiveButton("OK", (dialogInterface, i) -> {
                String editTextInput = editText.getText().toString();
            })
            .setNegativeButton("Cancel", null)
            .create();
    dialog.show();

Upvotes: 1

Serg Burlaka
Serg Burlaka

Reputation: 2496

The same on Kotlin:

    val inputEditTextField = EditText(requireActivity())
    val dialog = AlertDialog.Builder(requireContext())
        .setTitle("Title")
        .setMessage("Message")
        .setView(inputEditTextField)
        .setPositiveButton("OK") { _, _ ->
                val editTextInput = inputEditTextField .text.toString()
                Timber.d("editext value is: $editTextInput")
        }
        .setNegativeButton("Cancel", null)
        .create()
    dialog.show()

Upvotes: 9

Jeeva
Jeeva

Reputation: 1951

The reason you are not able to access editTextField is because of it is declared as local variable in alertDialog() method.

If in case you want to keep that variable as local ,you need to set the listener method directly in that method as i show below.

private EditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("Title")
                .setMessage("Message")
                .setView(inputEditTextField)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String editTextInput = inputEditTextField.getText().toString();
                        Log.d("onclick","editext value is: "+ editTextInput);
                    }
                })
                .setNegativeButton("Cancel", null)
                .create();
dialog.show();


Else you can replace this line from alertDialog() method with this below line

editTextField = new EditText(this.getContext());


you need add this line above onCreate method

EditTextField editTextField;

Upvotes: 10

Related Questions