Create an alert in Android

An easy way to create an alert in Android using AlertBuilder.


Libraries

Code Snippet
new AlertDialog.Builder(context)
    .setTitle("Title")
    .setMessage("Message")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // User clicked on the yes-Button
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // User clicked on the no-Button
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)  // Set icon
    .show();


Implementation

Create a delete-dialog: <code>new AlertDialog.Builder(context) .setTitle("Delete User") .setMessage("Are you sure to delete the user?") .setPositiveButton("Yes, delete!", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { DeleteUser(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Don't delete the user } }) .setIcon(android.R.drawable.btn_close) // Set close/delete icon .show();</code>





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Android
    Alert



    Informations

    posted on 2016-09-16 16:20:53

    viewed 36 times

    snippet's UUID is 57dbffc51976f

    Similar Snippets