How to create a Dialog box in Android - Part 1
Hi again! We are back with another tutorial on Android notifications. Make sure you go through the tutorial on Toast Notification before reading on.
In Android you can use a dialog box with one, two, or three buttons or none at all. You can also have a list of selectable items that consists of checkboxes or radio buttons.
For creating these dialog boxes we will be using AlertDialog which is an extension of Dialog class from Android API library. In this tutorial we will be creating four different types of dialog boxes -
- An alert box with a single button
- A dialog box with two buttons
- A dialog box containing a list
- A dialog box containing checkboxes

Note:
- We will be using Android Platform 1.5 for developing our application.
To demonstrate how to create dialog boxes firstly we will be creating a basic application with four buttons and clicking on each of these buttons will display a different kind of dialog box. We will be using XML based layout for our Application's User Interface. Create a new Android project with the following properties -
- Project Name - DialogBox
- Name of the Application - Dialog Box
- Package name - com.botskool.DialogBox
- Activity Name - Dialog Box
There will be four buttons and a string as listed below -
- information string
- alert button (This button will be used to display an alert box)
- yesno button (This will be used to display a dialog box with Yes/No buttons)
- selectlist button (This will be used to display a dialog box which contains a list)
- selectlistwithcheckbox (This will be used to display a dialog box which contains a list along with checkbox)
Copy the following code in DialogBox.java -
package com.botskool.DialogBox;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class DialogBox extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** We need to set up a click listener on the alert button */
Button alert = (Button) findViewById(R.id.alertbutton);
alert.setOnClickListener(this);
/** We need to set up a click listener on the yesno button */
Button yesno = (Button) findViewById(R.id.yesnobutton);
yesno.setOnClickListener(this);
/** We need to set up a click listener on the selectlist button */
Button selectlist = (Button) findViewById(R.id.selectlist);
selectlist.setOnClickListener(this);
/** We need to set up a click listener on the selectlistwithcheckbox button */
Button selectlistwithcheckbox = (Button) findViewById(R.id.selectlistwithcheckbox);
selectlistwithcheckbox.setOnClickListener(this);
}
public void onClick(View view) {
/** check whether the alert button has been clicked */
if (view == findViewById(R.id.alertbutton)) {
// Code here to display alert box
}
/** check whether the yesno button has been clicked */
if (view == findViewById(R.id.yesnobutton)) {
// Code here to display the dialog box with Yes/No button
}
/** check whether the selectlist button has been clicked */
if (view == findViewById(R.id.selectlist)) {
//Code here to display the dialog box containing a list.
}
/** check whether the selectlistwithcheckbox button has been clicked */
if (view == findViewById(R.id.selectlistwithcheckbox)) {
//Code here to display the dialog box with checkbox
}
}
}
Copy the following code in main.xml -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/information"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert"
android:id="@+id/alertbutton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Yes/No"
android:id="@+id/yesnobutton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show List"
android:id="@+id/selectlist"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show List with Checkbox"
android:id="@+id/selectlistwithcheckbox"
/>
</LinearLayout>
Copy the following code in strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="information">Various kind of dialog boxes - by www.bOtskOOl.com</string>
<string name="app_name">Dialog Box</string>
</resources>
Compile and run the Android application and you will get this -
/** check whether the alert button has been clicked */
// Create the alert box
// Set the message to display
// Add a neutral button to the alert box and assign a click listener
// Click listener on the neutral button of alert box
// The neutral button was clicked
// show the alert box
- Positive or Yes
- Negative or No
- Neutral or Cancel
You can ONLY add one of each button type to your AlertDialog box. So this limits the number of possible buttons to three: positive, neutral, and negative. As quoted on Android website - These names are technically irrelevant to the actual functionality of your buttons, but should help you keep track of which one does what.
Now if we want to create a dialog box with two buttons you just need to use some other type of button apart from neutral button. Say for Yes/No dialog box we use a positive and a negative button then we can their property by calling setPostiveButton() and setNegativeButton() as shown below -
/** check whether the yesno button has been clicked */
if (view == findViewById(R.id.yesnobutton)) {
// Create the dialog box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// Set the message to display
alertbox.setMessage("This is a dialog box with two buttons");
// Set a positive/yes button and create a listener
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// Click listener
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
}
});
// Set a negative/no button and create a listener
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
// Click listener
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
}
});
// display box
alertbox.show();
}
The functions setPostiveButton() and setNegativeButton() are similar to setNeutralButton(). Check out the screenshots below.











Recent comments
46 weeks 4 days ago
48 weeks 6 days ago
49 weeks 14 hours ago
49 weeks 6 days ago
51 weeks 4 days ago
1 year 1 day ago
1 year 1 day ago
1 year 4 weeks ago
1 year 4 weeks ago
1 year 4 weeks ago