Android alert dialog

Alert Dialog is a message box to the user. By default, the alert dialog does not close automatically. It remains interactive until there is an action that triggers the dialog to close. You can place one, two, or tree buttons on the alert dialog. The alert dialog can be created by using the AlertDialog.Builder class. After you create an instance of AlertDialog.Builder, you will specify parameters (e.g. message and buttons) for the alert dialog. Then call the create() method of AlertDialog.Builder instance to create the alert dialog. To show the dialog to the user you must call the show() method. The method below can be called any where in your activity to display an alert dialog.





public void showAlert(String title, String message){
  
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle(title); // title of the dialog
     builder.setMessage(message); // message to display when the dialog opens      
     builder.setCancelable(true); //allow cancellation
     builder.setPositiveButton("OK", null); //show OK button
     //add click event to the button    
     builder.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener(){
     public void onClick(DialogInterface dialog, int which) {
       dialog.dismiss(); //close the dialog when the OK button is pushed
       }

     });
    
     AlertDialog dialog = builder.create(); //create the dialog    
     dialog.show(); //show the dialog
  
   }

The alert dialog created above is a simple message box. If you need more complex message box, you must create a custom view and set the view to the dialog by using the setView method of the AlertDialog. Builder instance. For example, suppose that you want to display an image along with a message in red color. You can achieve this goal by creating a LinearyLayout layout object and add an ImageView and a TextView to the layout object. Then call the setView method to add the layout object to the dialog.

   public void showAlert(String title, String message){
  
   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setTitle(title);
   //builder.setMessage(message);
   builder.setCancelable(true);
   builder.setPositiveButton("OK", null);
    LinearLayout layout=new LinearLayout(this);
   ImageView image=new ImageView(this);
   image.setImageDrawable(this.getResources().getDrawable(R.drawable.info));
   TextView text=new TextView(this);
   text.setText(message);
   text.setTextColor(Color.RED);
   layout.addView(image);
   layout.addView(text);    
   builder.setView(layout);
   builder.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener(){
     public void onClick(DialogInterface dialog, int which) {
       dialog.dismiss();
       }

     });
    
     AlertDialog dialog = builder.create();      
     dialog.show();
  
   }

Android-alert-dialog

Posted by: Dara | post date: 05-15-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.