Reputation: 7949
I have the following AlertDialog
and its onClick
method:
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setView(View.inflate(this, R.layout.barcode_alert, null));
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
@Override
public void onClick(@NotNull DialogInterface dialog, int which) {
// how to get editTextField.getText().toString(); ??
dialog.dismiss();
}
});
The XML I inflate in the dialog (barcode_alert.xml
) contains, among other things, an EditText
field, and I need to get its string value after the user taps the Search button.
My question is, how do I get a reference to that field so I can get its text string?
Upvotes: 0
Views: 199
Reputation: 2473
You can inflate your view and get a reference to a variable:
View v = View.inflate(this, R.layout.barcode_alert, null);
you can use findViewById
to get a reference to your view. Make sure that your variable is final or a member variable. Otherwise you cannot access it in the onClickListener
final EditText editText = (EditText)v.findViewById(R.id.your_edit_text
Upvotes: 0
Reputation: 9569
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final View v = View.inflate(this, R.layout.barcode_alert, null); //here
alertDialog.setView(v);
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
@Override
public void onClick(@NotNull DialogInterface dialog, int which) {
((EditText)v.findViewById(R.id.edit_text)).getText().toString(); //and here
dialog.dismiss();
}
});
Upvotes: 2
Reputation: 1570
Try this,
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final View v = getLayoutInflater().inflate(this, R.layout.barcode_alert, null);
final EditText editTextField = (EditText) v.findViewById(R.id.edit_text);
editTextField.setOnClickListener(MyActivity.this);
alertDialog.setView(v);
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
@Override
public void onClick(@NotNull DialogInterface dialog, int which) {
String enteredValue = editTextField.getText().toString(); // get the text from edit text
dialog.dismiss();
}
});
Upvotes: 0