Some times in your application , if you wanted to ask the user about taking a decision between yes or no in response of any particular action taken by the user, by remaining in the same activity and without changing the screen, you can use Alert Dialog.
In order to make an alert dialog , you need to make an object of AlertDialogBuilder which an inner class of AlertDialog. Its syntax is given below:
1 |
AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this); |
Now you have to set the positive (yes) , negative (no) or neutral button using the object of the AlertDialogBuilder class. Its syntax is:
1 2 3 |
builder.setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) builder.setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) builder.setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) |
Apart from this , you can use other functions provided by the builder class to customize the alert dialog. These are listed below:
Method | Description |
---|---|
setIcon(Drawable icon) | This method set the icon of the alert dialog box. |
setCancelable(boolean cancel able) | This method sets the property that the dialog can be cancelled or not. |
setMessage(CharSequence message) | This method sets the message to be displayed in the alert dialog. |
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener) | This method sets list of items to be displayed in the dialog as the content. The selected option will be notified by the listener. |
setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) | This method Sets the callback that will be called if the dialog is cancelled. |
setTitle(CharSequence title) | This method set the title to be appear in the dialog. |
After creating and setting the dialog builder , you will create an alert dialog by calling the create() method of the builder class. Its syntax is:
1 2 |
AlertDialog alertDialog = builder.create(); alertDialog.show(); |
This will create the alert dialog and will show it on the screen.
In this example i will be demonstrating how to work on Android Alert Dialog.
Let’s get start by creating a project in Eclipse IDE.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)
2. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android_alertdialog_demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
3. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:padding="5dp" > <Button android:id="@+id/show_dialog1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/neutral_button" /> <Button android:id="@+id/show_dialog2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/positive_button" /> <Button android:id="@+id/show_dialog3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/negative_button" /> <Button android:id="@+id/show_dialog4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/list_dialog" /> <Button android:id="@+id/show_dialog5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/custom_dialog" /> </LinearLayout> |
4. Now, add the following code in MainActivity.java. In this class we displayed different types of alert dialogs on button clicks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
package com.android_alertdialog_demo; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Implementing click listeners on all buttons findViewById(R.id.show_dialog1).setOnClickListener(this); findViewById(R.id.show_dialog2).setOnClickListener(this); findViewById(R.id.show_dialog3).setOnClickListener(this); findViewById(R.id.show_dialog4).setOnClickListener(this); findViewById(R.id.show_dialog5).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.show_dialog1: // Calling Neutral button dialog showNeutralButtonDialog(); break; case R.id.show_dialog2: // Calling Positive button dialog showPositiveButtonDialog(); break; case R.id.show_dialog3: // Calling Negative button dialog showNegativeButtonDialog(); break; case R.id.show_dialog4: // Calling List Item dialog showListItemsDialog(); break; case R.id.show_dialog5: // Calling Custom dialog showCustomDialog(); break; case R.id.custom_button1: // Showing a toast on custom dialog button click Toast.makeText(MainActivity.this, "Custom Button 1 Presses", Toast.LENGTH_SHORT).show(); break; case R.id.custom_button2: Toast.makeText(MainActivity.this, "Custom Button 2 Presses", Toast.LENGTH_SHORT).show(); break; } } // Neutral button Dialog method void showNeutralButtonDialog() { // Making instance of alert dialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); // Setting title for dialog builder.setTitle("Neutral Button Dialog"); // Setting icon for dialog builder.setIcon(R.drawable.ic_warning_amber_36dp); // Setting true/false if we want to dismiss dialog on back pressing or // touching outside the dialog builder.setCancelable(true); // Setting message to be displayed inside dialog builder.setMessage("Press neutral button below."); // Implementing Neutral button and click listener over it builder.setNeutralButton("Neutral Button", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // Showing a toast on button click Toast.makeText(MainActivity.this, "Neutral Button Pressed.", Toast.LENGTH_SHORT) .show(); } }); // Creating alert dialog and showing it AlertDialog dialog = builder.create(); dialog.show(); } // Positive button Dialog method void showPositiveButtonDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Two Button Dialog"); builder.setIcon(R.drawable.ic_warning_amber_36dp); builder.setCancelable(true); builder.setMessage("This dialog contains 2 buttons."); // Implementing Neutral button and click listener over it builder.setNeutralButton("Neutral Button", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Neutral Button Pressed.", Toast.LENGTH_SHORT) .show(); } }); // Implementing Positive button and click listener over it builder.setPositiveButton("Positive Button", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Positive Button Pressed.", Toast.LENGTH_SHORT) .show(); } }); AlertDialog dialog = builder.create(); dialog.show(); } // Negative button Dialog method void showNegativeButtonDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Three Button Dialog"); builder.setIcon(R.drawable.ic_warning_amber_36dp); builder.setCancelable(true); builder.setMessage("This dialog contains 3 buttons."); // Implementing Neutral button and click listener over it builder.setNeutralButton("Neutral Button", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Neutral Button Pressed.", Toast.LENGTH_SHORT) .show(); } }); // Implementing Positive button and click listener over it builder.setPositiveButton("Positive Button", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Positive Button Pressed.", Toast.LENGTH_SHORT) .show(); } }); // Implementing Negative button and click listener over it builder.setNegativeButton("Negative Button", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Negative Button Pressed.", Toast.LENGTH_SHORT) .show(); } }); AlertDialog dialog = builder.create(); dialog.show(); } // List Item Dialog void showListItemsDialog() { // Items to be displayed in list String[] listItems = { "Items 1", "Items 2", "Items 3" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("List Items Dialog"); builder.setIcon(R.drawable.ic_warning_amber_36dp); builder.setCancelable(true); // Setting list items over dialog and there should be no message builder.setItems(listItems, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int pos) { Toast.makeText(MainActivity.this, "Item positon : " + pos, Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builder.create(); dialog.show(); } // Custom Dialog void showCustomDialog() { // Layout inflater for inflating layout LayoutInflater inflater = this.getLayoutInflater(); // Inflating layout View dialogView = inflater.inflate(R.layout.custom_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); // Setting custom view over dialog builder.setView(dialogView); // Implementing click listeners over custom buttons dialogView.findViewById(R.id.custom_button1).setOnClickListener(this); dialogView.findViewById(R.id.custom_button2).setOnClickListener(this); AlertDialog dialog = builder.create(); dialog.show(); } } |
5. Create a layout file for custom dialog under res ⇒ layout folder. I named the layout file as custom_dialog.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_dialog_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:gravity="center" android:orientation="vertical" android:padding="5dp" > <TextView android:id="@+id/dialog_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/custom_title" android:textColor="#FF00" android:textSize="20sp" /> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#ff00" /> <TextView android:id="@+id/dilaog_message" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/custom_message" android:textColor="#000000" android:textSize="15sp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/custom_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" android:text="@string/custom_button1" /> <Button android:id="@+id/custom_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" android:text="@string/custom_button2" /> </LinearLayout> </LinearLayout> |
6. Run the application and you will get the output as shown in video and you are done.
Thanks.
Subscribe to us and get the latest news.