stormin986 stormin986 - 10 months ago 103
Android Question

How to implement a custom AlertDialog View

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(;
fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));

First off, it's pretty obvious that
is a typo and is meant to be

I'm confused by the first line using 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 get defined or assigned or whatever?

Here's my code. I tried to use
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);
.setPositiveButton("Go", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {
EditText textBox = (EditText) findViewById(;

FrameLayout f1 = (FrameLayout)findViewById( /*CURRENTLY an ERROR*/);

AlertDialog alert = builder.create();;


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:

LayoutInflater inflater = getLayoutInflater();
FrameLayout f1 = (FrameLayout)alert.findViewById(;
f1.addView(inflater.inflate(R.layout.dialog_view, f1, false));