Pages

Search This Blog

Thursday, 9 August 2012

CREATING ALERT DIALOG BOX IN ANDROID

Dialogs are normally used for notifications that should interrupt the user and to perform short tasks that directly relate to the application in progress.
AlertDialog
Consider a user sending a message, if user clicks Send button before specifying a phone number the application should display a dialog box.

//text filed is named as phoneno, where user types phone no.
EditText phn=(EditText)findViewById(R.id.phoneno);
String no=phn.getText().toString();

if (no.isEmpty())
{
   AlertDialog diabox=makeAndShowDialogBox();
   diabox.show();
}
//creating dialog box

public AlertDialog makeAndShowDialogBox()
    {
    final AlertDialog nonumberDialogBox=new AlertDialog.Builder(this).create();
    nonumberDialogBox.setTitle("Alert!");//sets title for dialog
    nonumberDialogBox.setMessage("Please specify a phone number");//sets message for dialog
    nonumberDialogBox.setIcon(R.drawable.no);//sets icon for dialog,icon file is placed under drawable folder
    nonumberDialogBox.setButton("OK", new DialogInterface.OnClickListener() {
//adds a OK button to dialog
@Override
public void onClick(DialogInterface arg0, int arg1) {
nonumberDialogBox.cancel();//closes dialog when Ok button is pressed
}
});
    return nonumberDialogBox;
    }
Output

Application developers can also add two or three buttons to a dialog box, each performing a different function


1 comment:

  1. I simply want to say I’m very new to blogs and actually loved you’re blog site. Almost certainly I’m going to bookmark your blog post . You absolutely come with great well written articles. Thanks a lot for sharing your blog.
    Android Training in chennai with placement | Android Training in velachery

    ReplyDelete