pipiem555
Memory Leak Specialist
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
Operating System
Android
We create an AlertDialog by using its Builder class, whose methods all return an instance of Builder.
AN ALERT WITH ONE BUTTON
We set the alert dialog title
We set the alertDialog message
We set the alert dialog Icon
Finally we create OK button and set onClick listener to the button
We show the dialog.
AN ALERT WITH TWO BUTTONS
You can create three types of buttons:-
1.setPositiveButton
Describes an intended action or a confirmation, e.g. “Save” or “Yes”
2.setNeutralButton
Describes a “neutral” action such as “Close”.
3.setNegativeButton
Describes a cancellation or simply a “No”.
We create the Yes button and set the onClick listener
AN ALERT WITH THREE BUTTONS
Here is the complete source code for the above tutorial
Main.xml file
Dialog.java
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
Android
We create an AlertDialog by using its Builder class, whose methods all return an instance of Builder.
AN ALERT WITH ONE BUTTON
- AlertDialog alertDialog =
new
AlertDialog.Builder
(
Dialog
.this
)
.create
(
)
;
We set the alert dialog title
- alertDialog.setTitle
(
"Alert Dialog"
)
;
We set the alertDialog message
- alertDialog.setMessage
(
"Welcome to Android"
)
;
We set the alert dialog Icon
- alertDialog.setIcon
(
R.drawable
.tick
)
;
Finally we create OK button and set onClick listener to the button
- alertDialog.setButton
(
"OK"
, new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog, int
which)
- {
- Toast.makeText
(
getApplicationContext(
)
,"You clicked on OK"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- }
)
;
We show the dialog.
- alertDialog.show
(
)
;
AN ALERT WITH TWO BUTTONS
You can create three types of buttons:-
1.setPositiveButton
Describes an intended action or a confirmation, e.g. “Save” or “Yes”
2.setNeutralButton
Describes a “neutral” action such as “Close”.
3.setNegativeButton
Describes a cancellation or simply a “No”.
- AlertDialog.Builder
alertDialog =
new
AlertDialog.Builder
(
Dialog
.this
)
;
- alertDialog.setTitle
(
"Confirm Delete..."
)
;
- alertDialog.setMessage
(
"Are you sure you want delete this?"
)
;
- alertDialog.setIcon
(
R.drawable
.delete
)
;
We create the Yes button and set the onClick listener
- alertDialog.setPositiveButton
(
"YES"
,new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- Toast.makeText
(
getApplicationContext(
)
, "You clicked on YES"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- }
)
;
- We create the Yes button and set the onClick listener
- alertDialog.setNegativeButton
(
"NO"
,new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- Toast.makeText
(
getApplicationContext(
)
, "You clicked on NO"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- dialog.cancel
(
)
;
- }
- }
)
;
- alertDialog.show
(
)
;
AN ALERT WITH THREE BUTTONS
- btnAlertThreeBtns.setOnClickListener
(
new
View
.OnClickListener
(
)
- {
- public
void
onClick(
View
arg0)
- {
- AlertDialog.Builder
alertDialog =
new
AlertDialog.Builder
(
Dialog
.this
)
;
- alertDialog.setTitle
(
"Save File..."
)
;
- alertDialog.setMessage
(
"Do you want to save this file?"
)
;
- // Setting Icon to Dialog
- alertDialog.setIcon
(
R.drawable
.save
)
;
- //Here we set the Yes setPositiveButton
- alertDialog.setPositiveButton
(
"YES"
,new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog, int
which)
- {
- Toast.makeText
(
getApplicationContext(
)
,"You clicked on YES"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- }
- }
)
;
- // Setting setNegativeButton No
- alertDialog.setNeutralButton
(
"NO"
,new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- Toast.makeText
(
getApplicationContext(
)
,"You clicked on NO"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- }
- }
)
;
- alertDialog.setNegativeButton
(
"Cancel"
,new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- Toast.makeText
(
getApplicationContext(
)
,"You clicked on
- Cancel"
,Toast.LENGTH_SHORT
)
.show
(
)
;
- }
- }
)
;
- alertDialog.show
(
)
;
Here is the complete source code for the above tutorial
Main.xml file
- <?
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"
- android:
background=
"#ffffff"
- >
- <
Button
- android:
id=
"@+id/btnAlert"
- android:
layout_width=
"match_parent"
- android:
layout_height=
"wrap_content"
- android:
text=
"Show Alert"
- android:
layout_marginRight=
"40dp"
- android:
layout_marginLeft=
"40dp"
>
- </
Button>
- <
Button
- android:
id=
"@+id/btnAlertWithTwoBtns"
- android:
layout_width=
"match_parent"
- android:
layout_height=
"wrap_content"
- android:
text=
"Show Alert With Two Buttons"
- android:
layout_marginRight=
"40dp"
- android:
layout_marginLeft=
"40dp"
- >
- </
Button>
- <
Button
- android:
id=
"@+id/btnAlertWithThreeBtns"
- android:
layout_width=
"match_parent"
- android:
layout_height=
"wrap_content"
- android:
text=
"Show Alert with Three Buttons"
- android:
layout_marginRight=
"40dp"
- android:
layout_marginLeft=
"40dp"
- >
- </
Button>
- </
LinearLayout>
Dialog.java
- package
com.dialog
;
- import
android.app.Activity
;
- import
android.app.AlertDialog
;
- import
android.content.DialogInterface
;
- import
android.os.Bundle
;
- import
android.view.View
;
- import
android.widget.Button
;
- import
android.widget.Toast
;
- public
class
Dialog
extends
Activity
- {
- @Override
- public
void
onCreate(
Bundle savedInstanceState)
- {
- super
.onCreate
(
savedInstanceState)
;
- setContentView(
R.layout
.main
)
;
- Button
btnAlert =
(
Button
)
findViewById(
R.id
.btnAlert
)
;
- Button
btnAlertTwoBtns =
(
Button
)
findViewById(
R.id
.btnAlertWithTwoBtns
)
;
- Button
btnAlertThreeBtns =
(
Button
)
findViewById(
R.id
.btnAlertWithThreeBtns
)
;
- btnAlert.setOnClickListener
(
new
View
.OnClickListener
(
)
- {
- public
void
onClick(
View
arg0)
- {
- // Creating alert Dialog with one Button
- AlertDialog alertDialog =
new
- AlertDialog.Builder
(
Dialog
.this
)
.create
(
)
;
- // Setting Dialog Title
- alertDialog.setTitle
(
"Alert Dialog"
)
;
- // Setting Dialog Message
- alertDialog.setMessage
(
"Welcome to Android"
)
;
- // Setting Icon to Dialog
- alertDialog.setIcon
(
R.drawable
.tick
)
;
- // Setting OK Button
- alertDialog.setButton
(
"OK"
, new
- DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- // Write your code here to execute after dialog closed
- Toast.makeText
(
getApplicationContext(
)
,"You clicked on OK"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- }
- }
)
;
- // Showing Alert Message
- alertDialog.show
(
)
;
- }
- }
)
;
- btnAlertTwoBtns.setOnClickListener
(
new
View
.OnClickListener
(
)
- {
- public
void
onClick(
View
arg0)
- {
- // Creating alert Dialog with two Buttons
- AlertDialog.Builder
alertDialog =
new
- AlertDialog.Builder
(
Dialog
.this
)
;
- // Setting Dialog Title
- alertDialog.setTitle
(
"Confirm Delete..."
)
;
- // Setting Dialog Message
- alertDialog.setMessage
(
"Are you sure you want
- delete this?"
)
;
- // Setting Icon to Dialog
- alertDialog.setIcon
(
R.drawable
.delete
)
;
- // Setting Positive "Yes" Button
- alertDialog.setPositiveButton
(
"YES"
,new
- DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- // Write your code here to execute after dialog
- Toast.makeText
(
getApplicationContext(
)
, "You clicked on YES"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- }
- }
)
;
- // Setting Negative "NO" Button
- alertDialog.setNegativeButton
(
"NO"
, new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- // Write your code here to execute after dialog
- Toast.makeText
(
getApplicationContext(
)
, "You clicked on NO"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- dialog.cancel
(
)
;
- }
- }
)
;
- // Showing Alert Message
- alertDialog.show
(
)
;
- }
- }
)
;
- btnAlertThreeBtns.setOnClickListener
(
new
View
.OnClickListener
(
)
- {
- public
void
onClick(
View
arg0)
- {
- // Creating alert Dialog with three Buttons
- AlertDialog.Builder
alertDialog =
new
AlertDialog.Builder
(
Dialog
.this
)
;
- // Setting Dialog Title
- alertDialog.setTitle
(
"Save File..."
)
;
- // Setting Dialog Message
- alertDialog.setMessage
(
"Do you want to save this file?"
)
;
- // Setting Icon to Dialog
- alertDialog.setIcon
(
R.drawable
.save
)
;
- // Setting Positive Yes Button
- alertDialog.setPositiveButton
(
"YES"
,new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- // User pressed Cancel button. Write Logic Here Toast.makeText(getApplicationContext(),"You clicked on YES", Toast.LENGTH_SHORT).show();
- }
- }
)
;
- // Setting Positive Yes Button
- alertDialog.setNeutralButton
(
"NO"
,new
- DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- Toast.makeText
(
getApplicationContext(
)
,"You clicked on NO"
,
- Toast.LENGTH_SHORT
)
.show
(
)
;
- }
- }
)
;
- // Setting Positive "Cancel" Button
- alertDialog.setNegativeButton
(
"Cancel"
,new
DialogInterface.OnClickListener
(
)
- {
- public
void
onClick(
DialogInterface dialog,int
which)
- {
- Toast.makeText
(
getApplicationContext(
)
,"You clicked on
- Cancel"
,Toast.LENGTH_SHORT
)
.show
(
)
;
- }
- }
)
;
- alertDialog.show
(
)
;
- }
- }
)
;
- }
- }
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
You must upgrade your account or reply in the thread to view hidden text.