Professional Documents
Culture Documents
AlertDialog box and how to deal with those dialogs. Dialog is a small window which takes a
small part in your screen. It is majorly used to react to user driven events. Dialogs are always
part of your activity. It gets the focus unless the user closes it. Android dialogs are used for many
purposes such as,
Dialog class is the base class for all dialog types. The
subclasses of Dialog class are,
1. AlertDialog
2. Progress Dialog
3. DatePickerDialog
4. TimePickerDialog
We should create the instance of one of the above subclasses to create a dialog.
Structure of a Dialog
A simple android dialog box has the following components.
Title
o This component is optional. You can omit it if your content area has a simple
message that needs to be shown to the user as below.If your content area is
designed to ask user confirmation or if it needs any user input then it is a good
practice to add a title.
Content area
o This component can contain,
A message
A list
Custom layout
Action buttons
o This component is used to collect user response(Example “OK”, “Cancel” etc
buttons). There can be maximum three action buttons.
Let us now see how we can create a simple AlertDialog box in android:
Our main.xml contains a relative layout which in turn contains an Edittext which has a default
text “AlertDialod Demo” and a button named “Clear data”.
Step 4: Create the Activities
package my.app;
2 import android.app.Activity;
3 import android.app.AlertDialog;
4 import android.content.DialogInterface;
5 import android.content.Intent;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.TextView;
12 import android.widget.Toast;
13
14 public class Dialog extends Activity {
15 Button Clear;
16 EditText edit_data;
17 /** Called when the activity is first created. */
18 @Override
19 public void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22
23 Clear=(Button)findViewById(R.id.Button01);
24 edit_data=(EditText)findViewById(R.id.EditText01);
25
26 Clear.setOnClickListener(new OnClickListener() {
27 @Override
28 public void onClick(View v) {
29 // TODO Auto-generated method stub
30 AlertDialog.Builder builder=new AlertDialog.Builder(Dialog.this);
31 //Set a title
32 builder.setTitle("Confirm");
33 //Set a message
34 builder.setMessage("Do you want to Clear?");
35
36 builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
37
38 @Override
39 public void onClick(DialogInterface dialog, int which) {
40 // TODO Auto-generated method stub
41
42 //clearing the contents of edittext on click of OK button
43 edit_data.setText("");
44 //Displaying a toast message
Toast.makeText(getApplicationContext(), "Your text has been cleared",
45
Toast.LENGTH_LONG).show();
46
47 }
48 });
49 builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
50
51 @Override
52 public void onClick(DialogInterface dialog, int which) {
53 // TODO Auto-generated method stub
54 dialog.cancel();
55 }
56 });
57
58 //Create the dialog
59 AlertDialog alertdialog=builder.create();
60
61 //show the alertdialog
62 alertdialog.show();
63
64 }
65 });
66 }
67 }
As you can see in the above code the dialog is created using following steps:
1 builder.setTitle("Confirm");
builder.setNegativeButton("Cancel",new
1
DialogInterface.OnClickListener()
2 {
3 @Override
4 public void onClick(DialogInterface dialog, int which)
5 {
6 // TODO Auto-generated method stub
7 dialog.cancel();
8 }
9 });
1 builder.setNeutralButton("NO",new DialogInterface.OnClickListener()
2 {
3 @Override
4 public void onClick(DialogInterface dialog, int which)
5 {
6 // TODO Auto-generated method stub
7 //Do something
8 }
9 });
1 builder.setIcon(R.drawable.alert);