Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Custom AlertDialog ? What is the root view?

Tags:

android

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.

like image 865
Code Droid Avatar asked May 22 '12 02:05

Code Droid


People also ask

How do I get view from AlertDialog?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view.

Which method is used to set in AlertDialog?

setIcon() method is use to set the icon on Alert dialog box.

What is the correct syntax for creating an object of AlertDialog builder to make an alert dialog?

setTitle(CharSequence title)AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.

How many button options can you use in creating AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.


1 Answers

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.

like image 168
Czechnology Avatar answered Sep 19 '22 15:09

Czechnology