• We just launched and are currently in beta. Join us as we build and grow the community.

Alert Dialog

pipiem555

Memory Leak Specialist
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
130
Likes
130
Bits
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

  1. AlertDialog alertDialog =

    new

    AlertDialog.Builder

    (

    Dialog

    .this

    )

    .create

    (

    )

    ;

We set the alert dialog title
  1. alertDialog.setTitle

    (

    "Alert Dialog"

    )

    ;

We set the alertDialog message
  1. alertDialog.setMessage

    (

    "Welcome to Android"

    )

    ;

We set the alert dialog Icon
  1. alertDialog.setIcon

    (

    R.drawable

    .tick

    )

    ;

Finally we create OK button and set onClick listener to the button
  1. alertDialog.setButton

    (

    "OK"

    , new

    DialogInterface.OnClickListener

    (

    )
  2. {

  3. public

    void

    onClick(

    DialogInterface dialog, int

    which)
  4. {

  5. Toast.makeText

    (

    getApplicationContext(

    )

    ,"You clicked on OK"

    ,
  6. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  7. }

    )

    ;

We show the dialog.
  1. 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”.

  1. AlertDialog.Builder

    alertDialog =

    new

    AlertDialog.Builder

    (

    Dialog

    .this

    )

    ;
  2. alertDialog.setTitle

    (

    "Confirm Delete..."

    )

    ;
  3. alertDialog.setMessage

    (

    "Are you sure you want delete this?"

    )

    ;
  4. alertDialog.setIcon

    (

    R.drawable

    .delete

    )

    ;

We create the Yes button and set the onClick listener

  1. alertDialog.setPositiveButton

    (

    "YES"

    ,new

    DialogInterface.OnClickListener

    (

    )
  2. {
  3. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  4. {
  5. Toast.makeText

    (

    getApplicationContext(

    )

    , "You clicked on YES"

    ,
  6. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  7. }

    )

    ;

  8. We create the Yes button and set the onClick listener

  9. alertDialog.setNegativeButton

    (

    "NO"

    ,new

    DialogInterface.OnClickListener

    (

    )
  10. {
  11. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  12. {

  13. Toast.makeText

    (

    getApplicationContext(

    )

    , "You clicked on NO"

    ,
  14. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  15. dialog.cancel

    (

    )

    ;
  16. }
  17. }

    )

    ;
  18. alertDialog.show

    (

    )

    ;

AN ALERT WITH THREE BUTTONS

  1. btnAlertThreeBtns.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )
  2. {

  3. public

    void

    onClick(

    View

    arg0)
  4. {

  5. AlertDialog.Builder

    alertDialog =

    new

    AlertDialog.Builder

    (

    Dialog

    .this

    )

    ;
  6. alertDialog.setTitle

    (

    "Save File..."

    )

    ;

  7. alertDialog.setMessage

    (

    "Do you want to save this file?"

    )

    ;
  8. // Setting Icon to Dialog
  9. alertDialog.setIcon

    (

    R.drawable

    .save

    )

    ;

  10. //Here we set the Yes setPositiveButton
  11. alertDialog.setPositiveButton

    (

    "YES"

    ,new

    DialogInterface.OnClickListener

    (

    )
  12. {

  13. public

    void

    onClick(

    DialogInterface dialog, int

    which)
  14. {
  15. Toast.makeText

    (

    getApplicationContext(

    )

    ,"You clicked on YES"

    ,
  16. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  17. }
  18. }

    )

    ;

  19. // Setting setNegativeButton No
  20. alertDialog.setNeutralButton

    (

    "NO"

    ,new

    DialogInterface.OnClickListener

    (

    )
  21. {

  22. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  23. {
  24. Toast.makeText

    (

    getApplicationContext(

    )

    ,"You clicked on NO"

    ,
  25. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  26. }
  27. }

    )

    ;
  28. alertDialog.setNegativeButton

    (

    "Cancel"

    ,new

    DialogInterface.OnClickListener

    (

    )
  29. {

  30. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  31. {
  32. Toast.makeText

    (

    getApplicationContext(

    )

    ,"You clicked on
  33. Cancel"

    ,Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  34. }
  35. }

    )

    ;
  36. alertDialog.show

    (

    )

    ;

Here is the complete source code for the above tutorial
Main.xml file

  1. <?

    xml version=

    "1.0"

    encoding=

    "utf-8"

    ?>
  2. <

    LinearLayout xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  3. android:

    orientation=

    "vertical"
  4. android:

    layout_width=

    "fill_parent"
  5. android:

    layout_height=

    "fill_parent"
  6. android:

    background=

    "#ffffff"
  7. >

  8. <

    Button


  9. android:

    id=

    "@+id/btnAlert"
  10. android:

    layout_width=

    "match_parent"
  11. android:

    layout_height=

    "wrap_content"
  12. android:

    text=

    "Show Alert"
  13. android:

    layout_marginRight=

    "40dp"
  14. android:

    layout_marginLeft=

    "40dp"

    >
  15. </

    Button>

  16. <

    Button


  17. android:

    id=

    "@+id/btnAlertWithTwoBtns"
  18. android:

    layout_width=

    "match_parent"
  19. android:

    layout_height=

    "wrap_content"
  20. android:

    text=

    "Show Alert With Two Buttons"
  21. android:

    layout_marginRight=

    "40dp"
  22. android:

    layout_marginLeft=

    "40dp"
  23. >
  24. </

    Button>

  25. <

    Button


  26. android:

    id=

    "@+id/btnAlertWithThreeBtns"
  27. android:

    layout_width=

    "match_parent"
  28. android:

    layout_height=

    "wrap_content"
  29. android:

    text=

    "Show Alert with Three Buttons"
  30. android:

    layout_marginRight=

    "40dp"
  31. android:

    layout_marginLeft=

    "40dp"
  32. >
  33. </

    Button>
  34. </

    LinearLayout>

Dialog.java

  1. package

    com.dialog

    ;
  2. import

    android.app.Activity

    ;
  3. import

    android.app.AlertDialog

    ;
  4. import

    android.content.DialogInterface

    ;
  5. import

    android.os.Bundle

    ;
  6. import

    android.view.View

    ;
  7. import

    android.widget.Button

    ;
  8. import

    android.widget.Toast

    ;

  9. public

    class

    Dialog

    extends

    Activity
  10. {
  11. @Override
  12. public

    void

    onCreate(

    Bundle savedInstanceState)
  13. {
  14. super

    .onCreate

    (

    savedInstanceState)

    ;
  15. setContentView(

    R.layout

    .main

    )

    ;

  16. Button

    btnAlert =

    (

    Button

    )

    findViewById(

    R.id

    .btnAlert

    )

    ;
  17. Button

    btnAlertTwoBtns =

    (

    Button

    )

    findViewById(

    R.id

    .btnAlertWithTwoBtns

    )

    ;
  18. Button

    btnAlertThreeBtns =

    (

    Button

    )

    findViewById(

    R.id

    .btnAlertWithThreeBtns

    )

    ;

  19. btnAlert.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )
  20. {

  21. public

    void

    onClick(

    View

    arg0)
  22. {
  23. // Creating alert Dialog with one Button

  24. AlertDialog alertDialog =

    new
  25. AlertDialog.Builder

    (

    Dialog

    .this

    )

    .create

    (

    )

    ;

  26. // Setting Dialog Title
  27. alertDialog.setTitle

    (

    "Alert Dialog"

    )

    ;

  28. // Setting Dialog Message
  29. alertDialog.setMessage

    (

    "Welcome to Android"

    )

    ;

  30. // Setting Icon to Dialog
  31. alertDialog.setIcon

    (

    R.drawable

    .tick

    )

    ;

  32. // Setting OK Button
  33. alertDialog.setButton

    (

    "OK"

    , new
  34. DialogInterface.OnClickListener

    (

    )
  35. {

  36. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  37. {

  38. // Write your code here to execute after dialog closed
  39. Toast.makeText

    (

    getApplicationContext(

    )

    ,"You clicked on OK"

    ,
  40. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  41. }
  42. }

    )

    ;

  43. // Showing Alert Message
  44. alertDialog.show

    (

    )

    ;

  45. }
  46. }

    )

    ;

  47. btnAlertTwoBtns.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )
  48. {

  49. public

    void

    onClick(

    View

    arg0)
  50. {
  51. // Creating alert Dialog with two Buttons

  52. AlertDialog.Builder

    alertDialog =

    new
  53. AlertDialog.Builder

    (

    Dialog

    .this

    )

    ;

  54. // Setting Dialog Title
  55. alertDialog.setTitle

    (

    "Confirm Delete..."

    )

    ;

  56. // Setting Dialog Message
  57. alertDialog.setMessage

    (

    "Are you sure you want
  58. delete this?"

    )

    ;

  59. // Setting Icon to Dialog
  60. alertDialog.setIcon

    (

    R.drawable

    .delete

    )

    ;

  61. // Setting Positive "Yes" Button
  62. alertDialog.setPositiveButton

    (

    "YES"

    ,new
  63. DialogInterface.OnClickListener

    (

    )
  64. {
  65. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  66. {
  67. // Write your code here to execute after dialog
  68. Toast.makeText

    (

    getApplicationContext(

    )

    , "You clicked on YES"

    ,
  69. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  70. }
  71. }

    )

    ;
  72. // Setting Negative "NO" Button
  73. alertDialog.setNegativeButton

    (

    "NO"

    , new

    DialogInterface.OnClickListener

    (

    )
  74. {

  75. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  76. {
  77. // Write your code here to execute after dialog
  78. Toast.makeText

    (

    getApplicationContext(

    )

    , "You clicked on NO"

    ,

  79. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  80. dialog.cancel

    (

    )

    ;
  81. }
  82. }

    )

    ;
  83. // Showing Alert Message
  84. alertDialog.show

    (

    )

    ;

  85. }
  86. }

    )

    ;






  87. btnAlertThreeBtns.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )
  88. {

  89. public

    void

    onClick(

    View

    arg0)
  90. {
  91. // Creating alert Dialog with three Buttons

  92. AlertDialog.Builder

    alertDialog =

    new

    AlertDialog.Builder

    (

    Dialog

    .this

    )

    ;
  93. // Setting Dialog Title
  94. alertDialog.setTitle

    (

    "Save File..."

    )

    ;
  95. // Setting Dialog Message
  96. alertDialog.setMessage

    (

    "Do you want to save this file?"

    )

    ;
  97. // Setting Icon to Dialog
  98. alertDialog.setIcon

    (

    R.drawable

    .save

    )

    ;
  99. // Setting Positive Yes Button
  100. alertDialog.setPositiveButton

    (

    "YES"

    ,new

    DialogInterface.OnClickListener

    (

    )
  101. {

  102. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  103. {
  104. // User pressed Cancel button. Write Logic Here Toast.makeText(getApplicationContext(),"You clicked on YES", Toast.LENGTH_SHORT).show();
  105. }
  106. }

    )

    ;
  107. // Setting Positive Yes Button
  108. alertDialog.setNeutralButton

    (

    "NO"

    ,new
  109. DialogInterface.OnClickListener

    (

    )
  110. {

  111. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  112. {
  113. Toast.makeText

    (

    getApplicationContext(

    )

    ,"You clicked on NO"

    ,
  114. Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  115. }
  116. }

    )

    ;
  117. // Setting Positive "Cancel" Button
  118. alertDialog.setNegativeButton

    (

    "Cancel"

    ,new

    DialogInterface.OnClickListener

    (

    )
  119. {

  120. public

    void

    onClick(

    DialogInterface dialog,int

    which)
  121. {
  122. Toast.makeText

    (

    getApplicationContext(

    )

    ,"You clicked on

  123. Cancel"

    ,Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  124. }
  125. }

    )

    ;

  126. alertDialog.show

    (

    )

    ;

  127. }
  128. }

    )

    ;
  129. }
  130. }

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.
 

452,292

323,690

323,699

Top