Skip to main content

AlertBox Example in android

Alert Box in android 

A dialogue is a small window that prompts the user to make decision or enter additional information.A dialogue does not fill the screen and is normally used for modal events that require users to take an action before they can proceed
package com.example;

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 AlertExampleActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button button=(Button)findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {

                        new AlertDialog.Builder(AlertExampleActivity.this).
                        setTitle("AlertExample").setMessage("Do you want to exit").
                        setPositiveButton("YES", new DialogInterface.OnClickListener() {

                              public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(getApplicationContext(), "YES selected", Toast.LENGTH_LONG)
                                    .show();
                              }
                        }).
                        setNegativeButton("NO", new android.content.DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(getApplicationContext(), "NO selected", Toast.LENGTH_LONG)
                                    .show();
                              }
                        }).setIcon(R.drawable.icon).show();

                  }
            });
      }
}
Result

See more
Custom Alert box 

Comments