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 .
<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.
Button option_button=(Button)findViewById(R.id.option);
now on click listener we will call a alert dialog box
@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
We will now define a new xml in res/layout, ne.xml
<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
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
Stay Tuned for more Android Tips
Latest posts by sunny (see all)
- 5 Android Applications You Should Have – 4 Jan 2013 - January 5, 2013
- 5 Android Applications You Should Have - December 17, 2012
- How to use NDK for running C/C++ code in Android Apps - December 1, 2012








December 28th, 2011 at 4:12 am
I think you are Android Developer.
Trying to learn it.
anyway , nice article. keep it up !
gyanendra
December 28th, 2011 at 6:22 pm
yes i am working on it . pretty new though