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

Dialogs

A Dialog is small window that prompts the user to a decision.

Dialog

Some times in your application, if you wanted to ask the user about taking a decision between yes or no in response of any particular action taken by the user, you can use Alert Dialog.
Creates a builder for an alert dialog that uses the default alert dialog theme.
Android AlertDialog has three regions: title, content area and action buttons.

<?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>


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();
			}
		});
	}
}

Android - Layout