Creating Custom AlertDialog ? What is the root view?

android

Solution

Even though an older question, this article might be useful for others who search for this answer:

Layout Inflation as Intended:

If you’ve ever written something like the following code using LayoutInflater in your Android application:

inflater.inflate(R.layout.my_layout, null);

PLEASE read on, because you’re doing it wrong and I want to explain to you why.

...BUT...

Every Rule Has An Exception

There are of course instances where you can truly justify a `null` parent during inflation, but they are few. One such instance occurs when you are inflating a custom layout to be attached to an `AlertDialog`. Consider the following example where we want to use our same XML layout but set it as the dialog view:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
View content = LayoutInflater.from(context).inflate(R.layout.item_row, null);

builder.setTitle("My Dialog");
builder.setView(content);
builder.setPositiveButton("OK", null);
builder.show();

The issue here is that AlertDialog.Builder supports a custom view, but does not provide an implementation of setView() that takes a layout resource; so you must inflate the XML manually. However, because the result will go into the dialog, which does not expose its root view (in fact, it doesn’t exist yet), we do not have access to the eventual parent of the layout, so we cannot use it for inflation. It turns out, this is irrelevant, because AlertDialog will erase any LayoutParams on the layout anyway and replace them with match_parent.

The article has an explanation on why you should supply a parent `ViewGroup` in most other cases than Dialog building.

Problem

what i am trying to do: Create a custom Alert Dialog. Buttons just like any Alert Dialog but above are two TextEdit input boxes. I don't want to create a custom Dialog but a customized Alert Dialog Here is what I am trying #3: http://developer.android.com/guide/topics/ui/dialogs.html It says: ``` AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = getApplicationContext(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello, this is a custom dialog!"); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create(); ``` Documentation says: ``` View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); ``` where the first parameter is the layout resource ID and the second is the ID of the root View. Problem is I don't know what the layout root is? this is a dialog I am going to kick of in an Activity. Should I use the layout id if the activity? Is layout_root pulled out of a hat? Also tried: ``` View layout = inflater.inflate(R.layout.my_custom_layout, (ViewGroup) findViewById(android.R.id.content).getRootView()); ``` result null pointer.

Original source