Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning A Value From AlertDialog

I want to build a function that creates an AlertDialog and returns the string that the user entered, this the function I have for creating the dialog, how do I return the value?

String m_Text = "";
private String openDialog(String title) {
    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
    builder.setTitle(title);

    final EditText input = new EditText(view.getContext());
    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
    builder.setView(input);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            m_Text = input.getText().toString();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.show();

// return string
} 
like image 414
OmerN Avatar asked Apr 15 '16 15:04

OmerN


People also ask

How do I return AlertDialog?

you can't get the return value for alertbutton click without clicking on it. so you need to do something on alert button clicks. call any function on button clicks with alert button values.

How does AlertDialog return value in flutter?

You can access and use the value that comes from your dialog option like this: showDialog( context: context, builder: (context) => Dialog( val: vale, ), ). then((valueFromDialog){ // use the value as you wish print(valueFromDialog); });

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

How do I get view from AlertDialog?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

The call builder.show() which opens your AlertDialog is not a blocking call, meaning the next instructions will be executed without waiting for the AlertDialog to finish (return). The way you should interact with it is by using some sort of callback. For instance, your OnClickListeners are an implementation of such a pattern.

A simple callback pattern

One clean way to achieve what you want is to create a Functional Interface which is an interface having only one method. You would use it for handling your callbacks.

Example

interface OnOK{
    void onTextEntered(String text);
}

And then you would alter you method to be like:

private void openDialog(String title, final OnOK onOK) {
    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
    builder.setTitle(title);

    final EditText input = new EditText(view.getContext());
    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
    builder.setView(input);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
          //Oi, look at this line!
          onOK.onTextEntered(input.getText().toString());
       }
    });

    builder.show();
} 

You can use it like this:

openDialog("Title", new OnOK() {
   @Override
   onTextEntered(String text) {
      Log.i("LOG", text);
   } 
});
like image 144
Anis LOUNIS Avatar answered Oct 25 '22 16:10

Anis LOUNIS