How to display an alert box in Android. See flowing Steps :
- First, use the
AlertDialog.Builder
to create the alert box interface, like title, message to display, buttons, and button onclick function - Later attach above builder to
AlertDialog
and display it. - Done.
package com.Katchup.android; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { final Context context = this; private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.buttonAlert); // add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context); // set title alertDialogBuilder.setTitle("Your Title"); // set dialog message alertDialogBuilder .setMessage("Click yes to exit!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, close // current activity MainActivity.this.finish(); } }) .setNegativeButton("No",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } }); } }
No comments:
Post a Comment