Posts Tagged Alert Dialog in android
Dialog in Android
Posted by Deepthi Girija Thampi in Android on January 25, 2012
It is a small window that appears before the activity and then the activity loses attention and the Dialog gains all user focus. They are of different types:
1)Alert Dialog
2)Progress Dialog
3)Date picker
4)Time Picker
In this article I would like to discuss about Alert Dialog.
Dialog is the base class for creating dialogs. We usually use its subclasses for creating dialog depending on the type of dialogs we use in our applications.
The subclass we use for creating alert dialog is AlertDialog.
First of all we need to create a Builder object of the class AlertDialog.Builder.
Here it is,
AlertDialog.Builder builder1=new AlertDialog.Builder(MyAlertdialogActivity.this);
where MyAlertDialogActivity is my activity here.
Next is setting the message for your dialog.
Here,
builder1.setMessage("Please Press Ok");
Now, I’ll jot down the code for alert dialog with one button. We use the method setNeutralButton() for that as shown below.
builder1.setNeutralButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "Ok is pressed", Toast.LENGTH_LONG).show();
}
});
Here, DialogInterface in the code is used to allow some code to be run when an item on the dialog is clicked. Here I’ve given a Toast which says ‘Ok is pressed’ when the neutral button ‘OK’ is clicked.
Hope this made everything in the code so far clear.
Finally, we need to create the Alert Dialog with the features defined(ie,the message,the button ,Toast message etc) and display the same. So, the code for that is
builder1.show();
The code written together looks like this:
btnalert1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stubAlertDialog.Builder builder1=new AlertDialog.Builder(MyAlertdialogActivity.this);
builder1.setMessage("Please Press Ok");
builder1.setNeutralButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "Ok is pressed", Toast.LENGTH_LONG).show();
}
});
builder1.show();
}
});
where btnalert1 is the Button which on clicking displays the alert dialog.
And the output looks like this:
When Ok is clicked the screen looks like this:
We can also create Alert Dialog with Buttons side-by-side. Here is the code,
AlertDialog.Builder builder2=new AlertDialog.Builder(MyAlertdialogActivity.this);
builder2.setMessage("Press OK or Cancel");
builder2.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "U Clicked OK", Toast.LENGTH_LONG).show();
}
});
builder2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "U Clicked Cancel ", Toast.LENGTH_LONG).show();
}
});
builder2.show();
This is how the output looks:
When the OK button is clicked:
Now just check how we can create an Alert Dialog with a list of choices. The code for the same is
final CharSequence[] items={"One","two","three"};
btnalertlist.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder3=new AlertDialog.Builder(MyAlertdialogActivity.this);
builder3.setTitle("Pick your choice").setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "U clicked "+items[which], Toast.LENGTH_LONG).show();
}
});
builder3.show();
}
});
This is how the output for this looks:
When one is chosen, the screen looks like this:
The whole code is given below:
package com.deepthi.alertdialog; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MyAlertdialogActivity extends Activity { /** Called when the activity is first created. */ Button btnalert1,btnalert2,btnalertlist; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnalert1=(Button)findViewById(R.id.btndialog1); btnalert1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder builder1=new AlertDialog.Builder(MyAlertdialogActivity.this); builder1.setMessage("Please Press Ok"); builder1.setNeutralButton("OK",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Ok is pressed", Toast.LENGTH_LONG).show(); } }); builder1.show(); } }); btnalert2=(Button)findViewById(R.id.btndialog2); btnalert2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder builder2=new AlertDialog.Builder(MyAlertdialogActivity.this); builder2.setMessage("Press OK or Cancel"); builder2.setPositiveButton("OK",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "U Clicked OK", Toast.LENGTH_LONG).show(); } }); builder2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "U Clicked Cancel ", Toast.LENGTH_LONG).show(); } }); builder2.show(); } }); btnalertlist=(Button)findViewById(R.id.btndialog3); final CharSequence[] items={"One","two","three"}; btnalertlist.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder builder3=new AlertDialog.Builder(MyAlertdialogActivity.this); builder3.setTitle("Pick your choice").setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "U clicked "+items[which], Toast.LENGTH_LONG).show(); } }); builder3.show(); } }); } }
I’m planning to post articles regarding Progress Dialog, Date Picker and Time Picker also.So keep reading and also share your experiences working with android.