Android Java : How to wait for user input from a dialog. Alternate solution
using a custom interface.
Have you ever wanted to create a dialog and had the need to wait for
user input from the dialog then resume execution? In Android, this is not possible because
you cannot pause the UI Thread. Instead we can define our own interface and pass it to our promptForResult();
function, which will execute code after the "Ok" or "Cancel" button is clicked!
Here is a workaround:
1) create an interface to handle dialog events.
2) create a function that creates the dialog and accpets our custom interface.
3) call that function when needed to prompt for input.
Create the interface
interface DialogInputInterface {
// onBuildDialog() is called when the dialog builder is ready to accept a view to insert
// into the dialog
View onBuildDialog();
// onCancel() is called when the user clicks on 'Cancel'
void onCancel();
// onResult(View v) is called when the user clicks on 'Ok'
void onResult(View v);
}
Function to create our dialog:
void promptForResult(String dlgTitle, String dlgMessage, final DialogInputInterface dlg) {
// replace "MyClass.this" with a Context object. If inserting into a class extending Activity,
// using just "this" is perfectly ok.
Builder alert = new AlertDialog.Builder(MyClass.this);
alert.setTitle(dlgTitle);
alert.setMessage(dlgMessage);
// build the dialog
final View v = dlg.onBuildDialog();
// put the view obtained from the interface into the dialog
if (v != null) { alert.setView(v);}
// procedure for when the ok button is clicked.
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// ** HERE IS WHERE THE MAGIC HAPPENS! **
dlg.onResult(v);
dialog.dismiss();
return;
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dlg.onCancel();
dialog.dismiss();
return;
}
});
alert.show();
}
Example of how to use the interface
promptForResult("Input", "Please input a value.", new DialogInputInterface(){
// put references to any and all controls or Views you need here.
// also references to layout inflater view is helpful if using the layout inflater.
EditText input;
// Here is where you would build a "View" to return to the dialog. You can produce the view
// object by using the layout inflater to inflate an xml resource or you can create controls
// purely programically.
// If inflating a layout, use view.findViewById(R.id.view); to get references to View controls
// in your layout. It is important that you use view.findViewById(); and not this.findViewById();
// In our example, we simply insert an Textbox
public View onBuildDialog() {
// procedure to build the dialog view
input = new EditText(MyClass.this);
// cast the edittext as a view
View outView = (View) input;
// return the view to the dialog builder
return outView;
}
public void onCancel() {
// user has canceled the dialog by hitting cancel
}
// put whatever code you want to run after user enters a result
public void onResult(View v) {
// get the value from the dialog
String value = input.getText().toString();
// do something with this value...
// In our example we are taking our value and passing it to
// an activity intent, then starting the activity.
Intent i = new Intent(getApplication(), MyActivity.class);
i.putExtra("extraValue", value);
startActivity(i);
}
});
Published by: Thomas Penwell
Initially published on: July 25, 2014
Article last modified on: Thursday, September 3, 2015.