1

I am a new Android dev with a potentially dumb question.
In my activity create I want to make a popup dialogue box to prompt for user input. I then want to take the that input and save it as a global variable for later use. Currently I have

 public class MgenActivity extends Activity {

// Instance Variables
String ip = "";

/**
 * On Create
 */
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    String box = dialogBoxStart();
    this.ip = box;
    //more code 
    }

public String dialogBoxStart() 
{
    String returned = "";
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("MGEN Setup");
    alert.setMessage("Please enter the MGEN IP address such as \n 9.42.68.69");

    // Set an EditText view to get user input
    final EditText input = new EditText(this);
    alert.setView(input);

    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() 
    {
            public void onClick(DialogInterface dialog, int whichButton) 
            {
            //send user input to global?
            //this doesnt work: this.ip  = input.getText().toString();
            }
    };

    alert.setPositiveButton("Ok", listener);

    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });
    returned =  input.getText().toString();
    alert.show();
    return returned;

}

The problem is that my EditText 'input' is not saving the user input so I cannot save it outwards. As well, I cannot talk to my outside variables from within my onClick method.

So TLDR: How do you save input from a dialogue box?

Bggreen
  • 123
  • 4
  • 10

3 Answers3

1

It as simple as that.

DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        ip = input.getText().toString();
    }
};
Androiderson
  • 16,865
  • 6
  • 62
  • 72
0

Dialog don't block code execution or UI thread. You show the dialog, user clicks ok button you set value to the global variable, but your code which uses it is already executed. The same is with you current implementation. dialogBoxStart() returns empty value, because it gets input value before user enters anything.

Check this question: Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)

Community
  • 1
  • 1
Robertas
  • 26
  • 3
0
new AlertDialog.Builder(Main.this)
    .setTitle("Update Status")
    .setMessage(message)
    .setView(input)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            Editable value = input.getText(); 
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do nothing.
        }
    }).show();
Ruban
  • 1,514
  • 2
  • 14
  • 21