Android Custom Dialog Example

Moving ahead from simple dialog example, we are going to see about how to display a custom dialog using 2 Textview & 1 Button.
Custom Dialog is very much needed when we are using different UI Design graphics in Android. If the application top & bottom navigation bar color is blue, the dialog box also need to be blue to match the UI design. In this case we need a custom dialog with blue color with our own views.

Files Used :-

  • ( extends Activity - Main Activity Class )
  • ( extends Dialog - dialog box actions )
  • main.xml ( custom dialog design in xml file )

The output looks similar to


Design the dialog using xml file
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="fill_parent" xmlns:android=""
android:background="@drawable/bg_android" android:orientation="vertical"
android:gravity="center" android:layout_width="200dip">
<TextView android:id="@+id/TextView01" android:layout_height="wrap_content"
android:textColor="#fff" android:textStyle="bold"
android:layout_width="wrap_content" android:text=" "></TextView>
<TextView android:id="@+id/TextView02" android:layout_height="wrap_content"
android:textColor="#fff" android:layout_width="wrap_content"
android:layout_margin="7dip" android:text="Custom Dialog Example By"
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="OK" android:id="@+id/OkButton"></Button>
This is Main Activity Class, used to display a custom dialog.
[sourcecode language="java"]
package org.androidpeople.dialog;

import android.os.Bundle;

public class CustomDialogExample extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
/** Display Custom Dialog */
CustomizeDialog customizeDialog = new CustomizeDialog(this);;
This class should be extends with Dialog. Dialog views action will be done here.
[sourcecode language="java"]
package org.androidpeople.dialog;

import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

/** Class Must extends with Dialog */
/** Implement onClickListener to dismiss dialog when OK Button is pressed */
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;

public CustomizeDialog(Context context) {
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
/** Design the dialog in main.xml file */
okButton = (Button) findViewById(;

public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)


You can download the full source code from here