Now lets discuss a small topic which may be used sometimes.
An Alert Box......................
Sometimes we may come across few alert boxes saying Do you want to continue with a single button or two or three buttons.
Alert box, as the name suggests it alerts and interrupts the user to perform short tasks that are related to the application.
When alert box is displayed the current task loses focus and the dialog accepts the whole focus.
This simply means that we can interact only with the alert box and not with the previous task.
Now lets get into the details how to display an alert dialog box in android.
An Alert box can be done either through java code or inflating an xml file
While we are inflating an xml file we can have any views other than buttons in the alert box. (This is a custom alert box).
- 1.A simple way to create an alert dialog is to set the theme property for the activity which you want to display as a alert dialog.
android:theme="@android:style/Theme.Dialog"
- Now lets discuss how it can be done using java code.
Firstly do the following :
1. AlertDialog.Builder builder = new AlertDialog.Builder(YourClassName.this);
Here we are creating a Builder object for the current context. This will be used in the following steps.
If you observe we can see the following :
- Title to the alert dialog box (ALERT)
- Message in the Alert dialog Box (this is a alert box.Click yes/no)
- And finally two buttons yes and no.
As i said the Builder object is used in the following steps, the title , message and the buttons are set using the builder object.
To set the title to the dialog box, we can use the following line line :
builder.setTitle("ALERT");
To set a message to the alert dialog box, we use
builder.setMessage("this is an alert box. Click Yes/No");
And the 'yes' and 'no' buttons can be displayed and can be handled using the code :
- buillder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DisplayAlert.this, "You clicked yes",
- Toast.LENGTH_SHORT).show();
- }
- });
And the other button for 'no' can be dislayed and handled using a similar code as above.
- builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DisplayAlert.this, "You clicked no",
- Toast.LENGTH_SHORT).show();
- dialog.dismiss();
- }
- });
Finally we need to create and show the alert box which can be done using the following lines :
- AlertDialog alertdialog = builder.create();
- alertdialog.show();
Now let us see a sample example where i am having a button and the click of the button an alert dialog box appears.
Following is the xml file :
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/main_BTN_display"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="DISPLAY" />
- </LinearLayout>
- 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;
- /**
- *
- *
- * Class that displays an alert dialog box and handles Yes/No buttons.
- */
- public class DisplayAlert extends Activity implements OnClickListener {
- Button mBTN_display;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mBTN_display = (Button) findViewById(R.id.main_BTN_display);
- mBTN_display.setOnClickListener(this);
- }
- @Override
- public void onClick(View arg0) {
- display_Alert();
- }
- /**
- * This method displays an alert dialog box and handles Yes/No buttons
- */
- private void display_Alert() {
- AlertDialog.Builder builder = new AlertDialog.Builder(DisplayAlert.this);
- builder.setTitle("ALERT");
- builder.setMessage("this is an alert box. Click Yes/No");
// Set an EditText view to get user input final EditText input = new EditText(this); builder.setView(input);
- builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DisplayAlert.this, "You clicked yes",
- Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DisplayAlert.this, "You clicked no",
- Toast.LENGTH_SHORT).show();
- dialog.dismiss();
- }
- });
- AlertDialog alertdialog = builder.create();
- alertdialog.show();
- }
- }
There is no change in the manifest file.
Thus on running the application we can get a alert dialog on click of the button.
Any suggestions are welcomed.
Please comment if anyone has some or the other problem with this one.
Comments
Post a Comment