Categorized | Android, How To

How to Inflate Alert Dialog Box with Another Layout

Posted on 04 December 2011 by sunny

Lets Start by  making a simple  alert dialog box .

In main.xml I am defining a button on click of which we will launch alert dialog box .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button
android:id="@+id/option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>

now in our main activity we will bind button id and declare alertdialog box.

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
Button option_button=(Button)findViewById(R.id.option);



now  on click listener we will call a alert dialog box

option_button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(AlertDialogSamples.this);

alertbox.setMessage("Simple Alert Dialog box"); // Message to be displayed

alertbox.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}

});

alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}

});

// show the alert box will be swapped by other code later
alertbox.show();
}
});

we will have something like this

simple alert dialog box

 

 

 

 

 

 

 

 

 

 

 

We will now define a new xml in res/layout, ne.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>

<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/prompt"
/>

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dead" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mango" />

</LinearLayout>

Now we will inflate the existing layout with our new layout in our main activity

       LayoutInflater li = getLayoutInflater();
View v = li.inflate(R.layout.ne, null);
builder.setView(v);

we will replace alertbox.show (); in main activity

by      builder.show(); and we will get

inalted alert dialog box

 

 

 

 

 

 

 

 


Stay Tuned for more Android Tips

sunny
Co-Founder of www.deadmango.com . Loves to read and write about emerging technologies, a small time open source enthusiast and a big time android lover.

2 Comments For This Post

  1. gyanendra Says:

    I think you are Android Developer.

    Trying to learn it.

    anyway , nice article. keep it up !

    gyanendra

  2. sunny Says:

    yes i am working on it . pretty new though :)

1 Trackbacks For This Post

  1. Android Alert Dialog Not Displaying Properly – Message is blank? | PHP Developer Resource Says:

    [...] to code sample here : Tagged: Androidquestions /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO [...]

Leave a Reply


one + 7 =

RELATED SITES

Free Games Play Now Cards, Jackpot City Casino Canada, Free.
casino games online 32Red Online Casino is one of few casinos where PayPal account holders can


Australian Online Gambling 2013 - Learn about Australian online gambling and pokie machine accepted at online casinos and is especially popular with Canadian players.
Lucky 88 is a poker machine royal vegas play at online best online casinos Safe Online Casino | Best Online Casinos – Proven Safety - Big Bonus!
The best casino online australia Over 200 free pokie games played directly in your browser. However, the best online australian pokies download All Slots casino pokies for Australian players. the best option for Canadian online gamblers, and we explain. pokies games online feature trustworthy gaming venues where you can play online pokies and slots.
kruskie_milissa@mailxu.com