I’ve a simple program to show a dialog, with a edittext view in it, and listen to positive/negative buttons, to perform a custom action in each (read that edittext and save its content to an activity variable).
The problem arises when I can’t see any way to recover my current dialog from dialog interface (and then, I can’t recover any view within dialog).
Probably it’s a noob question, but after some google searches, I’ve no one answer to it.
My code is the following
LayoutInflater li = LayoutInflater.from(this);
View myView = li.inflate(R.layout.my_layout, null);
AlertDialog.Builder cDialog = new AlertDialog.Builder(this);
cDialog.setView(myView);
cDialog.setPositiveButton(R.string.start_download, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//here the code to retrieve dialog
}
});
cDialog.create();
Any clue about where can I find that?
You can retrieve the views from the Dialog
in the onClick()
with:
EditText edit = (EditText) ((AlertDialog) dialog).findViewById(R.id.the_id_of_view);
Answer:
This way runs perfect in my code:
public void onClick(DialogInterface dialog, int which) {
Dialog dialog2 =Dialog.class.cast(dialog);
EditText edit = (EditText) dialog2.findViewById(R.id.myedittext);
}
cheers
Answer:
You already have reference to the View that has the edit text. Why not use it directly? Just make sure you make the view final so that you can access it in the OnClickListener
anonymous class.
LayoutInflater li = LayoutInflater.from(this);
final View myView = li.inflate(R.layout.my_layout, null);
// don't forget to mark myView as final
AlertDialog.Builder cDialog = new AlertDialog.Builder(this);
cDialog.setView(myView);
cDialog.setPositiveButton(R.string.start_download, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//here the code to retrieve dialog
EditText edit = (EditText) myView.findViewById(R.id.the_id_of_view);
}
});
cDialog.create();
Answer:
You can get the Dialog returned from method show()
alertBuidler.
AlertDialog.Builder adb = new AlertDialog.Builder(YourActivity.this);
//...code to add methods setPositive an setNegative buttons
Call the show()
method of adb
and get Dialog
as below:
final AlertDialog dialog = adb.show();
So you can call any button of your dialog at any point of code in your activity:
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();//or
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();//or
dialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick();
Answer:
You can get the dialog instance with the following code:
val builder = AlertDialog.Builder(this)
builder.setMessage(R.string.eneter_url_message)
.setView(R.layout.input_dialog)
.setPositiveButton(R.string.OK) { dialog, _ -> validateUrl(dialog) }
.setNegativeButton(R.string.CANCEL) { dialog, _ -> dialog.dismiss() }
val dialog = builder.create()
dialog.show()
urlInput = dialog.findViewById(R.id.urlInput)
Once you call dialog.show() then you can find any inflated views with findViewById
Answer:
AlertDialog alertDialog = new AlertDialog.Builder(navigationDrawer.getContext())
.setView(R.layout.add_category_dialog)
.create();
alertDialog.show();
AppCompatSpinner spinner = alertDialog.findViewById(R.id.add_category_icon_selector_id);
Tags: androidandroid, login, view