My Life with Android
Introduction Android Componets UserInterface Advanced UI Data Storage Advanced Concepts Others New Studio

Alert Dailog

AlertDialog.Builder to create the alert box interface, like title, message to display, buttons, and button onclick function.
alertdialog_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show" />

</LinearLayout>
AlertDailogDemo.java
package com.example.androidcollegeppt;

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 AlertDailogDemo extends Activity{
	Button btnShow;
	AlertDialog.Builder builder;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alertdialog_demo);
		builder = new AlertDialog.Builder(this);
		builder.setTitle("This my title");
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	         @Override
	         public void onClick(DialogInterface arg0, int arg1) {
	            Toast.makeText(AlertDailogDemo.this,"You clicked yes button",Toast.LENGTH_LONG).show();
	         }
	      });
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
	         @Override
	         public void onClick(DialogInterface arg0, int arg1) {
	            Toast.makeText(AlertDailogDemo.this,"You clicked no button",Toast.LENGTH_LONG).show();
	         }
	      });
		btnShow=(Button)findViewById(R.id.btnShow);
		btnShow.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				builder.show();
			}
		});
	}
}