TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Add AlertDialog in Android
Abhijeet Singh
Apr 01, 2020
17.4k
0
2
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Print
Other Artcile
In this article we will see how to add an AlertDialog in Android.
AlertDialog.rar
Introduction
In this article, we will see how to add an AlertDialog in Android.
Procedure
Start the Eclipse IDE.
Create a new project.
Create a MainActivity.java file.
Create an activity_main.xml file for layout design.
Add a button in the XML layout.
Then look up the button by its id in the MainActivity.java file.
Create an object like this: AlertDialog.Builder a1=new AlertDialog.Builder(this);
The code for the example is given below.
MainActivity.java
package
com.example.aadialogtodayyy;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import
android.content.DialogInterface.OnClickListener;
import
android.os.Bundle;
import
android.os.Process;
import
android.view.KeyEvent;
import
android.view.View;
import
android.widget.Button;
public
class
MainActivity
extends
Activity
implements
android.view.View.OnClickListener
{
Button b1;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(
this
);
}
public
void
adddddd()
{
AlertDialog.Builder a1=
new
AlertDialog.Builder(
this
);
a1.setTitle(
"Exit from Abhijeet's Application"
);
a1.setMessage(
"Are u sure ?"
);
a1.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
a1.setPositiveButton(
"Yes"
,
new
OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
finish();
}
});
a1.setNegativeButton(
"No"
,
new
OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Process.killProcess(Process.myPid());
}
});
a1.show();
}
public
void
onClick(View v) {
// TODO Auto-generated method stub
adddddd();
}
@Override
public
boolean
onKeyDown(
int
keyCode, KeyEvent event) {
if
(keyCode==KeyEvent.KEYCODE_MENU)
{
adddddd();
}
// TODO Auto-generated method stub
return
super
.onKeyDown(keyCode, event);
}
}
Activity_main.xml
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:paddingBottom
=
"@dimen/activity_vertical_margin"
android:paddingLeft
=
"@dimen/activity_horizontal_margin"
android:paddingRight
=
"@dimen/activity_horizontal_margin"
android:paddingTop
=
"@dimen/activity_vertical_margin"
tools:context
=
".MainActivity"
>
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentTop
=
"true"
android:layout_marginLeft
=
"106dp"
android:layout_marginTop
=
"52dp"
android:text
=
"Click Here for Exit"
/>
</
RelativeLayout
>
Output
add AlertDialog in Android
AlertDialog
AlertDialog in Android
Android
Recommended Free Ebook
Printing in C# Made Easy
Download Now!
Similar Articles