Android alert dialog example
In this tutorial, we show you how to display an alert box in Android. See flowing Steps :
- First, use the
AlertDialog.Builder
to create the alert box interface, like title, message to display, buttons, and button onclick function - Later attach above builder to
AlertDialog
and display it. - Done.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
1 Android Layout Files
Simpel layout file, display a button on screen.
File : res/layout/main.xml
<?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/buttonAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert Box" />
</LinearLayout>
2. Activity
When user click on this button, display the alert box, with your pre-defined alert dialog interface.
File : MainActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonAlert);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
3. Demo
Start it, display a button.
When button is clicked, display the alert box
If “Yes” button is clicked, close the activity and return back to your Android main screen.
Download Source Code
Download it – Android-Alert-Dialogl-Example.zip (16 KB)
If i want to display the the alert dialog again i have a user/password login alert box and if authentication fails then display it again
What is the question? Just do it.
Thanks for giving us a simple and easy to understand example. But if you’re looking for more Android AlertDialog example, take a look at this http://www.codeofaninja.com/2011/07/android-alertdialog-example.html
Thank You , Thank You Very Much Teacher,,,,,,,,,,,,,,
PositiveButton has to be Yes and NegativeButton has to be no
Thanks working this code sir, thanks a lot sir.. ??
Superb..Sir Thanx lot Today is my first Day with Android and i learnt a lot…
Thanks mate, very concise.
Thanks a ton 🙂
Thank u very much
Thanks a lot, tutorial is great!
thanks for this great help
finally i can finish this project 🙂
Very well done. Thank you for taking the time to do this.
Thanks a lot, that helped me!
Thanks
Easy and quick… Thank you
Thanks! This really helped me out =]
thank you, it’s really helpful
Nice Work..Thanks Alot for Saving my time..
How to use intent in alert dialog box?
LOL you’re a faggot, cunt
Thank you so much. It helps a lot. God Bless you
This is working perfectly. But I have one question. Developer.android.com says alertDialogs can work in API>11. so how is this working in Android 2.3.3?
Hi, i think that i saw you visited my weblog so i came to _return the favor_.
I’m attempting to find things to improve my website!I suppose its ok to use some of your ideas!!
Thanks for the tips!
Cheers!
Thank you so much for this tutorial! It was as clear as I could hope for, and now I can make alerts!
Thank you VeRy much,
but can i make buttons vertically with this code??
i make it with this code,
——————————————————————————-
Context context = this;
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.context_layout2);
dialog.show();
———————————————————————————
but,
how can i use setOnClickListener on each of the buttons?
you can inflate the view first before adding it to the AlertDialog. in between that, you can add listeners there.
Thank you, it was really helpful.
Hi,
I’d like to override some methods of AlertDialog Class, therefore I Extend the AlertDialog Class:
instead of mkyong’s code:
Extended class:
When I run the app, I get a runtime error (What is wrong?):
02-09 02:17:35.709: W/dalvikvm(20492): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-09 02:17:35.709: E/AndroidRuntime(20492): FATAL EXCEPTION: main
02-09 02:17:35.709: E/AndroidRuntime(20492): java.lang.ClassCastException: android.app.AlertDialog
02-09 02:17:35.709: E/AndroidRuntime(20492): at com.example.filepicker3.FilePickerActivity.showFileDetailsDialogAlarm(FilePickerActivity.java:336)
02-09 02:17:35.709: E/AndroidRuntime(20492): at com.example.filepicker3.FilePickerActivity.onItemLongClick(FilePickerActivity.java:261)
02-09 02:17:35.709: E/AndroidRuntime(20492): at android.widget.AbsListView.performLongPress(AbsListView.java:1975)
02-09 02:17:35.709: E/AndroidRuntime(20492): at android.widget.AbsListView.access$600(AbsListView.java:74)
02-09 02:17:35.709: E/AndroidRuntime(20492): at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:1933)
02-09 02:17:35.709: E/AndroidRuntime(20492): at android.os.Handler.handleCallback(Handler.java:587)
02-09 02:17:35.709: E/AndroidRuntime(20492): at android.os.Handler.dispatchMessage(Handler.java:92)
02-09 02:17:35.709: E/AndroidRuntime(20492): at android.os.Looper.loop(Looper.java:123)
02-09 02:17:35.709: E/AndroidRuntime(20492): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-09 02:17:35.709: E/AndroidRuntime(20492): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 02:17:35.709: E/AndroidRuntime(20492): at java.lang.reflect.Method.invoke(Method.java:507)
02-09 02:17:35.709: E/AndroidRuntime(20492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-09 02:17:35.709: E/AndroidRuntime(20492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-09 02:17:35.709: E/AndroidRuntime(20492): at dalvik.system.NativeStart.main(Native Method)
The return object of
alertDialogBuilder.create();
is an AlertDialog object. An instance of AlertDialog can be cast to subclasses of AlertDialog. It can however be cast down to any class AlertDialog extends. It’s like saying a generic Object instance can be cast to anything. Essentially your approach is impossible. You’d have to make your own builder whose return is an instance of your class. Or work directly with methods provided to AlertDialog (and it’s subclasses). I suggest you do it mkyong’s way.
Thank you for the information, it really helped a lot.
Thank you, it was really helpful.