Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(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(); 
like image 531
stormin986 Avatar asked May 08 '10 19:05

stormin986


People also ask

How do I create a custom dialogue?

This example demonstrate about how to make custom dialog in android. 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.

How is DialogFragment implemented?

To create a DialogFragment , first create a class that extends DialogFragment , and override onCreateDialog() , as shown in the following example. Similar to how onCreateView() should create a root View in an ordinary fragment, onCreateDialog() should create a Dialog to display as part of the DialogFragment .


2 Answers

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:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:id="@+id/dialog_layout_root"               android:orientation="vertical"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:padding="10dp"               /> 

And then you can set your layout on the builder with the following code:

LayoutInflater inflater = getLayoutInflater(); View dialoglayout = inflater.inflate(R.layout.dialog_layout, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(dialoglayout); builder.show(); 
like image 185
Andrewx2 Avatar answered Oct 01 '22 09:10

Andrewx2


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(android.R.id.body); f1.addView(inflater.inflate(R.layout.dialog_view, f1, false)); 
like image 25
synic Avatar answered Oct 01 '22 10:10

synic