.
How to Show Alert Dialog in Android
1) Create New Blank Activitiy
Follow the wizard to create New Blank Activity.
2) Edit content_main.xml
| 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:tools="http://schemas.android.com/tools" 
   xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent" 
   android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin" 
   android:paddingRight="@dimen/activity_horizontal_margin" 
   android:paddingTop="@dimen/activity_vertical_margin" 
   android:paddingBottom="@dimen/activity_vertical_margin" 
   app:layout_behavior="@string/appbar_scrolling_view_behavior" 
   tools:showIn="@layout/activity_main" tools:context=".MainActivity"> 
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
       <Button android:id="@+id/btnAlert" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="Show Alert" 
           android:padding="10dip" 
           android:layout_marginTop="40dip"> 
       </Button> 
       <Button android:id="@+id/btnAlertWithTwoBtns" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="Show Alert With Two Buttons" 
           android:padding="10dip"> 
       </Button> 
       <Button android:id="@+id/btnAlertWithThreeBtns" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="Show Alert with Three Buttons" 
           android:padding="10dip"> 
       </Button> 
   </LinearLayout> 
</RelativeLayout> | 
Outcome.
3) Add Drawables
Download MyAlertDialog1-drawables.zip
4) Edit MainActivity.java
| 
package com.notarazi.myalertdialog1; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 
public class MainActivity extends AppCompatActivity { 
   @Override 
   protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
       setSupportActionBar(toolbar); 
       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( 
                       MainActivity.this).create(); 
               // Setting Dialog Title 
               alertDialog.setTitle("Alert Dialog"); 
               // Setting Dialog Message 
               alertDialog.setMessage("Welcome to AndroidHive.info"); 
               // 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(); 
           } 
       }); 
       /** 
        * Showing Alert Dialog with Two Buttons one Positive Button with Label 
        * "YES" one Negative Button with Label "NO" 
        */ 
       btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() { 
           public void onClick(View arg0) { 
               // Creating alert Dialog with two Buttons 
               AlertDialog.Builder alertDialog = newAlertDialog.Builder(MainActivity.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(); 
           } 
       }); 
       /** 
        * Showing Alert Dialog with Two Buttons one Positive Button with Label 
        * "YES" one Neutral Button with Label "NO" one Negative Button with 
        * label "Cancel" 
        */ 
       btnAlertThreeBtns.setOnClickListener(new View.OnClickListener() { 
           public void onClick(View arg0) { 
               // Creating alert Dialog with three Buttons 
               AlertDialog.Builder alertDialog = new AlertDialog.Builder( 
                       MainActivity.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) { 
                               // User pressed No button. Write Logic Here 
                               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) { 
                               // User pressed Cancel button. Write Logic Here 
                               Toast.makeText(getApplicationContext(), 
                                       "You clicked on Cancel", 
                                       Toast.LENGTH_SHORT).show(); 
                           } 
                       }); 
               // Showing Alert Message 
               alertDialog.show(); 
           } 
       }); 
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
       fab.setOnClickListener(new View.OnClickListener() { 
           @Override 
           public void onClick(View view) { 
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
                       .setAction("Action", null).show(); 
           } 
       }); 
   } 
   @Override 
   public boolean onCreateOptionsMenu(Menu menu) { 
       // Inflate the menu; this adds items to the action bar if it is present. 
       getMenuInflater().inflate(R.menu.menu_main, menu); 
       return true; 
   } 
   @Override 
   public boolean onOptionsItemSelected(MenuItem item) { 
       // Handle action bar item clicks here. The action bar will 
       // automatically handle clicks on the Home/Up button, so long 
       // as you specify a parent activity in AndroidManifest.xml. 
       int id = item.getItemId(); 
       //noinspection SimplifiableIfStatement 
       if (id == R.id.action_settings) { 
           return true; 
       } 
       return super.onOptionsItemSelected(item); 
   } 
} | 
4) Edit MainActivity.java
(Move implementation details to separate methods)
| 
package com.notarazi.myalertdialog1; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 
public class MainActivity extends AppCompatActivity { 
   @Override 
   protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
       setSupportActionBar(toolbar); 
       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) { 
               setDialogOneButton(); 
           } 
       }); 
       /** 
        * Showing Alert Dialog with Two Buttons one Positive Button with Label 
        * "YES" one Negative Button with Label "NO" 
        */ 
       btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() { 
           public void onClick(View arg0) { 
               setDialogTwoButtons(); 
           } 
       }); 
       /** 
        * Showing Alert Dialog with Two Buttons one Positive Button with Label 
        * "YES" one Neutral Button with Label "NO" one Negative Button with 
        * label "Cancel" 
        */ 
       btnAlertThreeBtns.setOnClickListener(new View.OnClickListener() { 
           public void onClick(View arg0) { 
               setDialogThreeButtons(); 
           } 
       }); 
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
       fab.setOnClickListener(new View.OnClickListener() { 
           @Override 
           public void onClick(View view) { 
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
                       .setAction("Action", null).show(); 
           } 
       }); 
   } 
   @Override 
   public boolean onCreateOptionsMenu(Menu menu) { 
       // Inflate the menu; this adds items to the action bar if it is present. 
       getMenuInflater().inflate(R.menu.menu_main, menu); 
       return true; 
   } 
   @Override 
   public boolean onOptionsItemSelected(MenuItem item) { 
       // Handle action bar item clicks here. The action bar will 
       // automatically handle clicks on the Home/Up button, so long 
       // as you specify a parent activity in AndroidManifest.xml. 
       int id = item.getItemId(); 
       //noinspection SimplifiableIfStatement 
       if (id == R.id.action_settings) { 
           return true; 
       } 
       return super.onOptionsItemSelected(item); 
   } 
   private void setDialogOneButton(){ 
       // Creating alert Dialog with one Button 
       AlertDialog alertDialog = new AlertDialog.Builder( 
               MainActivity.this).create(); 
       // Setting Dialog Title 
       alertDialog.setTitle("Alert Dialog"); 
       // Setting Dialog Message 
       alertDialog.setMessage("Welcome..."); 
       // 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(); 
   } 
   private void setDialogTwoButtons(){ 
       // Creating alert Dialog with two Buttons 
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.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(); 
   } 
   private void setDialogThreeButtons(){ 
       // Creating alert Dialog with three Buttons 
       AlertDialog.Builder alertDialog = new AlertDialog.Builder( 
               MainActivity.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) { 
                       // User pressed No button. Write Logic Here 
                       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) { 
                       // User pressed Cancel button. Write Logic Here 
                       Toast.makeText(getApplicationContext(), 
                               "You clicked on Cancel", 
                               Toast.LENGTH_SHORT).show(); 
                   } 
               }); 
       // Showing Alert Message 
       alertDialog.show(); 
   } 
} | 
 
No comments:
Post a Comment