Wednesday, August 15, 2012

Android AlertDialog

Android AlertDialog
 A subclass of Dialog that can display one, two or three buttons. If you only want to display a String in this dialog box, use the setMessage() method. If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it:

a) show yes or no alertdialog
b) show firsttime popup alertdialog
c) show message alertdialog
d) show alert alertdialog
c) helper alertdialog
a) show yes or no alertdialog
This alert dialog represents for show yes or no question.
Sampele code

 public  void showYesNoPrompt(Context _context, String title, String message, OnClickListener onYesListener, OnClickListener onNoListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(_context);
        builder.setTitle(title);
        builder.setIcon(android.R.drawable.ic_dialog_info); // lame icon
        builder.setMessage(message);
        builder.setCancelable(false);
        builder.setPositiveButton("Yes", onYesListener);
        builder.setNegativeButton("No", onNoListener);
        builder.show();
      }
 b) show firsttime popup alertdialog
This dialog represents for alert the welcome message
Sample code
public  void openFirstTimePopup(final Context context) {
        final String html = "Thank you!";
        final AlertDialog.Builder builder = new AlertDialog.Builder(context).
        setCancelable(true).
        setTitle("Welcome!").
        setMessage(html).
        setNegativeButton("Close",null);
        final AlertDialog di = builder.create();
        di.show();
      }
 c) show message alertdialog 
This alert dialog represents for show message
Sample Code
  public  void showMessage(Context _context, String title, String message, int icon, DialogInterface.OnClickListener ackHandler) {
        AlertDialog.Builder builder = new AlertDialog.Builder(_context);
        builder.setTitle(title);
        builder.setMessage(Html.fromHtml(message));
        builder.setCancelable(false);
        builder.setPositiveButton("Ok", ackHandler);
        builder.setIcon(icon);
        builder.show();
      }
d) show alert alertdialog 
This dialog represents for show the error message to user.
Sample code
public static void showAlert(Context context, String title, String text,
              boolean showOk) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
            alertBuilder.setTitle(title);
            alertBuilder.setMessage(text);
            if (showOk)
              alertBuilder.setNeutralButton("ok", null);
            alertBuilder.create().show();
          }
c) helper alertdialog
This dialog represents for show the helper message to user.
Sample Code
 public  void helperAlertDialog(final Context ctx, final CharSequence title, final CharSequence message) {
        new AlertDialog.Builder(ctx)
       .setIcon(R.drawable.ic_launcher)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
        })
        .show();
        }

Full Source Code 
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
 *
 * @author vijayakumar
 *
 */
public class AndroidMADQAActivity extends Activity {
    Button btn1,btn2,btn3,btn4,btn5;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      btn1 = (Button)findViewById(R.id.button1);
      btn2 = (Button)findViewById(R.id.button2);
      btn3 = (Button)findViewById(R.id.button3);
      btn4 = (Button)findViewById(R.id.button4);
      btn5 = (Button)findViewById(R.id.button5);
      btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                 showYesNoPrompt(AndroidMADQAActivity.this,"Yes or No AlertDialog","Hello",null,null);
        }
    });
      btn2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
               openFirstTimePopup(AndroidMADQAActivity.this);
          }
      });
      btn3.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
               showMessage(AndroidMADQAActivity.this,"Show Message Alert Dialog","You Have Message",R.drawable.ic_launcher,null);
          }
      });
      btn4.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
               showAlert(AndroidMADQAActivity.this,"show alert dialog","Error",true);
          }
      });
      btn5.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
             helperAlertDialog(AndroidMADQAActivity.this,"Helper Dialog", "Helper dialog");
            
          }
      });
    }
   
    public  void showYesNoPrompt(Context _context, String title, String message, OnClickListener onYesListener, OnClickListener onNoListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(_context);
        builder.setTitle(title);
        builder.setIcon(android.R.drawable.ic_dialog_info); // lame icon
        builder.setMessage(message);
        builder.setCancelable(false);
        builder.setPositiveButton("Yes", onYesListener);
        builder.setNegativeButton("No", onNoListener);
        builder.show();
      }
    public  void openFirstTimePopup(final Context context) {
        final String html = "Thank you!";
        final AlertDialog.Builder builder = new AlertDialog.Builder(context).
        setCancelable(true).
        setTitle("Welcome!").
        setMessage(html).
        setNegativeButton("Close",null);

        final AlertDialog di = builder.create();
       
        di.show();
      }
    public  void showMessage(Context _context, String title, String message, int icon, DialogInterface.OnClickListener ackHandler) {
        AlertDialog.Builder builder = new AlertDialog.Builder(_context);
        builder.setTitle(title);
        builder.setMessage(Html.fromHtml(message));
        builder.setCancelable(false);
        builder.setPositiveButton("Ok", ackHandler);
        builder.setIcon(icon);
        builder.show();
      }

    public static void showAlert(Context context, String title, String text,
              boolean showOk) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
            alertBuilder.setTitle(title);
            alertBuilder.setMessage(text);
            if (showOk)
              alertBuilder.setNeutralButton("ok", null);
            alertBuilder.create().show();
          }
    public  void helperAlertDialog(final Context ctx, final CharSequence title, final CharSequence message) {
        new AlertDialog.Builder(ctx)
       .setIcon(R.drawable.ic_launcher)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
        })
        .show();
        }

}

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...