I have a button in android which on clicking produces an alert box. But i don’t know how to capture the selected value from the alert box. My code is
btn=(Button)findViewById(R.id.btn);
String[] items = new String[] {"name", "doc", "age"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, items);
btn.setText("Select One");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(context)
.setTitle("Select Airport")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
});
Please help. Thanks in advance!
In your code
argument of
onClickevent ofAlertDialogreturns the number of position of the item that is selected.For checking you just
Toasta message when you select any item fromAlertDialogCode :
You can refer Android’s Official Guide On Dialogs for more information about Dialogs in Android