The code has been broken into three files
a) main.xml
b) dialogtest.xml
c) dialogActivity.java
a) 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="fill_parent"
android:layout_height="wrap_content" android:text="Raise a Toast"
android:id="@+id/toast" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Raise an Alert"
android:id="@+id/alert" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Raise an Custom Dialog"
android:id="@+id/custom" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Raise an Progress Dialog"
android:id="@+id/progress" />
</LinearLayout>
b) dialogtest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="@+id/image" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="10dp"
android:src="@drawable/icon" />
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#FFF"
android:text="This is a custom dialog" />
</LinearLayout>
c) dialogActivity.java
package org.example.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
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 dialogActivity extends Activity implements OnClickListener{
Button toast, alert, progress, custom;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialize();
}
public void initialize()
{
toast = (Button)findViewById(R.id.toast);
alert = (Button)findViewById(R.id.alert);
progress = (Button)findViewById(R.id.progress);
custom = (Button)findViewById(R.id.custom);
toast.setOnClickListener(this);
alert.setOnClickListener(this);
progress.setOnClickListener(this);
custom.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.toast:
Toast.makeText(this, "Toast Raised", Toast.LENGTH_SHORT).show();
break;
case R.id.alert:
AlertDialog.Builder ar = new AlertDialog.Builder(this);
ar.setTitle("Alert Example");
ar.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
notify.this.finish();
}
});
ar.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
ar.create().show();
break;
case R.id.custom:
// Context mcontext = getApplicationContext();
Dialog d = new Dialog(this);
d.setContentView(R.layout.dialogtest);
d.setTitle("Custom Dialog");
d.show();
break;
case R.id.progress:
//ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
ProgressDialog dialog = new ProgressDialog(this);
dialog.show(this, "Progress", "Loading. Please wait...", false, true, new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
}
});
break;
default:
break;
}
}
}
Screenshots :
Toast:
Alert Dialog:
Custom Dialog :
Progress Dialog:









