In the Android docs on AlertDialog, it gives the following instruction and example for setting a custom view in an AlertDialog:
If you want to display a more complex view, look up the FrameLayout called "body" and add your view to it:
FrameLayout fl = (FrameLayout) findViewById(R.id.body);
fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
First off, it's pretty obvious that add()
is a typo and is meant to be addView()
.
I'm confused by the first line using R.id.body. It seems that it's the body element of the AlertDialog ... but I can't just enter that in my code b/c it gives a compile error. Where does R.id.body get defined or assigned or whatever?
Here's my code. I tried to use setView(findViewById(R.layout.whatever)
on the builder but it didn't work. I'm assuming because I didn't manually inflate it?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title")
.setCancelable(false)
.setPositiveButton("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText textBox = (EditText) findViewById(R.id.textbox);
doStuff();
}
});
FrameLayout f1 = (FrameLayout)findViewById(R.id.body /*CURRENTLY an ERROR*/);
f1.addView(findViewById(R.layout.dialog_view));
AlertDialog alert = builder.create();
alert.show();
After changing the ID it android.R.id.custom, I needed to add the following to get the View to display:
However, this caused the new View to render in a big parent view with no background, breaking the dialog box in two parts (text and buttons, with the new View in between). I finally got the effect that I wanted by inserting my View next to the message:
I found this solution by exploring the View tree with View.getParent() and View.getChildAt(int). Not really happy about either, though. None of this is in the Android docs and if they ever change the structure of the AlertDialog, this might break.
You can create your view directly from the Layout Inflater, you only need to use the name of your layout XML file and the ID of the layout in file.
Your XML file should have an ID like this:
And then you can set your layout on the builder with the following code:
You are correct, it's because you didn't manually inflate it. It appears that you're trying to "extract" the "body" id from your Activity's layout, and that won't work.
You probably want something like this:
Custom AlertDialog
This full example includes passing data back to the Activity.
Create a custom layout
A layout with an
EditText
is used for this simple example, but you can replace it with anything you like.custom_layout.xml
Use the dialog in code
The key parts are
setView
to assign the custom layout to theAlertDialog.Builder
This is the full code from the example project shown in the image above:
MainActivity.java
Notes
DialogFragment
subclass as is described in the documentation.See also
android.R.id.custom was returning null for me. I managed to get this to work in case anybody comes across the same issue,
For reference, R.layout.simple_password is :