Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return data from dialog

Tags:

android

dialog

I am using a dialog as a form for the user to input data. When they click the "OK" button, the dialog is closed, and I need to use the data that was entered. How can I reference this data in the activity once the dialog has closed?

like image 906
dfetter88 Avatar asked Jan 24 '26 21:01

dfetter88


1 Answers

Get it when the user presses the "OK" button

final EditText input = new EditText(this); // This could also come from an xml resource, in which case you would use findViewById() to access the input

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText().toString();
        mItem.setValue(value); // mItem is a member variable in your Activity
        dialog.dismiss();
    }
});
like image 63
idolize Avatar answered Jan 27 '26 10:01

idolize



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!