Watch Pre-recorded Live Shows Here
Why Join
Become a member
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
TECHNOLOGIES
ANSWERS
LEARN
NEWS
BLOGS
VIDEOS
INTERVIEW PREP
BOOKS
EVENTS
Training
Live
JOBS
MORE
CAREER
MEMBERS
Creating an Alert Dialog Box in Android
Ehtesham Mehmood
Updated date
Mar 31, 2020
17.3k
0
0
In this article you will learn how to make an Alert Dialog Box in Android.
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
Print
Other Artcile
Expand
Introduction
Hey guys how are you? Today I will show you how to make an alert dialog box in Android. In this tutorial, I am using the class reference here
Android. Builder
. Let's start.
Figure 1
Figure 2
Figure 3
AndroidManifest.xml File
This is your AndroidManifest File; replace it with your own:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
package
=
"com.example.alertdialog"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
uses-sdk
android:minSdkVersion
=
"8"
android:targetSdkVersion
=
"19"
/>
<
application
android:allowBackup
=
"true"
android:icon
=
"@drawable/ic_launcher"
android:label
=
"@string/app_name"
android:theme
=
"@style/AppTheme"
>
<
activity
android:name
=
"com.example.alertdialog.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
>
Main.xml(Layout File)
This is your layout file:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
EditText
android:id
=
"@+id/EditText01"
android:text
=
"I Love Android"
android:layout_height
=
"50dip"
android:layout_width
=
"180dip"
>
</
EditText
>
<
Button
android:id
=
"@+id/Button01"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_below
=
"@+id/EditText01"
android:text
=
"Clear Screen"
>
</
Button
>
</
RelativeLayout
>
MainActivity.java File
This is your main Java file in which you will code it:
package com.example.alertdialog;
package
com.example.alertdialog;
import
android.support.v7.app.ActionBarActivity;
import
android.support.v7.app.ActionBar;
import
android.support.v4.app.Fragment;
import
android.app.AlertDialog;
import
android.app.Dialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
import
android.os.Build;
public
class
MainActivity
extends
ActionBarActivity
{
Button Clear;
EditText edit_data;
//Called when the activity is first created
@Override
public
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
Clear=(Button)findViewById(R.id.Button01);
edit_data=(EditText)findViewById(R.id.EditText01);
Clear.setOnClickListener(
new
View.OnClickListener()
{
@Override
public
void
onClick(View v)
{
AlertDialog.Builder builder=
new
AlertDialog.Builder(v.getContext());
//Set a title
builder.setTitle(
"Confirm"
);
//Set a message
builder.setMessage(
"Do you want to Clear?"
);
////Here Set a listener to be called when the positive button of the dialog is pressed.
builder.setPositiveButton(
"OK"
,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface dialog,
int
which)
{
//clearing the contents of edittext on click of OK button
edit_data.setText(
""
);
//Displaying a toast message
Toast.makeText(getApplicationContext(),
"Your text has been cleared"
, Toast.LENGTH_LONG).show();
}
});
//Here Set a listener to be called when the negative button of the dialog is pressed.
builder.setNegativeButton(
"Cancel"
,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface dialog,
int
which)
{
// TODO Auto-generated method stub
dialog.cancel();
}
});
//Create the dialog
AlertDialog alertdialog=builder.create();
//show the alertdialog
alertdialog.show();
}
});
}
}
Summary
For any queries please comment. I hope you guys will like this article and share this article with the world for sharing knowledge.
Android Programming
Dialog Box in Android
make an alert dialog box
Next Recommended Reading
FEATURED ARTICLES
View All
TRENDING UP
01
Using List Data Structures In Python😋
02
Why SharePoint Framework (SPFx) Is Best for SharePoint Development
03
Introducing Carbon - Google's New Programming Language
04
Basic Authentication For Azure Functions (Open API) .Net 6
05
Using Tuples Data Structure In Python 😋
06
How To Receive Real-Time Data In .NET 6 Client Application Using SignalR
07
Clean Architecture In ASP.NET Core Web API
08
RabbitMQ Message Queue Using .NET Core 6 Web API
09
Building Web Apps with Blazor+MudBlazor - MVP Show ft. Miguel Teheran
10
CRUD Operations In Windows Applications Using C#
View All
Learn Internet of Things in 21 Days
Challenge yourself
Java Skill
Get Certified
Stratis Blockchain Developer