Dialog

Toast, Alert Dialog, Yes No Dialog, Progress Dialog

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Show Toast"
		android:id="@+id/toastbtn"></Button>
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Show Alert"
		android:id="@+id/alertbtn"></Button>
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Show Yes/No"
		android:id="@+id/yesnobtn"></Button>
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Show Progress"
		android:id="@+id/progressbtn"></Button>
</LinearLayout>

Activity File

package com.androidwork.androiddialogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
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 AndroidDialogDemoActivity extends Activity implements
		OnClickListener {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button toast = (Button) findViewById(R.id.toastbtn);
		toast.setOnClickListener(this);

		// set a click listener on the alert button
		Button alert = (Button) findViewById(R.id.alertbtn);
		alert.setOnClickListener(this);

		// set a click listener on the yesno button
		Button yesno = (Button) findViewById(R.id.yesnobtn);
		yesno.setOnClickListener(this);

		// set a click listener on the progress button
		Button progress = (Button) findViewById(R.id.progressbtn);
		progress.setOnClickListener(this);
	}

	public void onClick(View view) {
		// TODO Auto-generated method stub
		// which button is clicked?

		// the Toast button
		if (view == findViewById(R.id.toastbtn)) {
			// display the toast popup window
			Toast.makeText(this, "This is the Toast message", Toast.LENGTH_LONG)
					.show();
		}

		// the Alert button the activated
		if (view == findViewById(R.id.alertbtn)) {

			// prepare the alert box
			AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

			// set the message to display
			alertbox.setMessage("This is the alertbox!");

			// add a neutral button to the alert box and assign a click listener
			alertbox.setNeutralButton("Ok",
					new DialogInterface.OnClickListener() {

						// click listener on the alert box
						public void onClick(DialogInterface arg0, int arg1) {
							// the button was clicked
							Toast.makeText(getApplicationContext(),
									"OK button clicked", Toast.LENGTH_LONG)
									.show();
						}
					});

			// show it
			alertbox.show();
		}

		// the yesno button is clicked
		if (view == findViewById(R.id.yesnobtn)) {
			// prepare the alert box
			AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

			// set the message to display
			alertbox.setMessage("This is the alertbox!");

			// set a positive/yes button and create a listener
			alertbox.setPositiveButton("Yes",
					new DialogInterface.OnClickListener() {

						// do something when the button is clicked
						public void onClick(DialogInterface arg0, int arg1) {
							Toast.makeText(getApplicationContext(),
									"'Yes' button clicked", Toast.LENGTH_SHORT)
									.show();
						}
					});

			// set a negative/no button and create a listener
			alertbox.setNegativeButton("No",
					new DialogInterface.OnClickListener() {

						// do something when the button is clicked
						public void onClick(DialogInterface arg0, int arg1) {
							Toast.makeText(getApplicationContext(),
									"'No' button clicked", Toast.LENGTH_SHORT)
									.show();

						}
					});
			alertbox.show();
			// display box

		}

		// progress button clicked
		if (view == findViewById(R.id.progressbtn)) {
			// prepare the dialog box
			ProgressDialog dialog = new ProgressDialog(this);

			// make the progress bar cancelable
			dialog.setCancelable(false);

			// set a message text
			dialog.setMessage("Loading...");

			// show it
			dialog.show();
		}

	}

}

Output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s