Tutorials Android Android Custom Dialog Example

Android Custom Dialog Example

In this tutorial we show you how to create custom dialog in android with code sample.

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. The Dialog class is the base class for dialogs.

Creating a custom dialog in android involves below steps

  1. Crating a custom layout for dialog
  2. Attaching layout to dialog instance
  3. Show/Hide dialog

Crating a custom layout for dialog

Here in this example we are creating an simple layout with an ImgeView, TextView and a Button. You may like to add your own layout with different UI widgets you need with your custom styles.

custom_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:contentDescription="@drawable/ic_launcher"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image"
        android:layout_centerHorizontal="true"
        android:text="Dismiss" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button"
        android:layout_toRightOf="@+id/image"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Attaching layout to dialog instance

Now, let us go back to our activity and create an instance of Dialog. We can attach our custom layout to dialog by using setContentView() method call.

package com.javatechig;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private Button button;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				final Dialog dialog = new Dialog(MainActivity.this);

				//setting custom layout to dialog
				dialog.setContentView(R.layout.cusotm_dialog_layout);
				dialog.setTitle("Custom Dialog");

				//adding text dynamically
				TextView txt = (TextView) dialog.findViewById(R.id.textView);
				txt.setText("Put your dialog text here.");

				ImageView image = (ImageView)dialog.findViewById(R.id.image);
				image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));

				//adding button click event
				Button dismissButton = (Button) dialog.findViewById(R.id.button);
				dismissButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
				dialog.show();
			}
		});
	}
}

Show/Hide dialog

When you want to show your dialog, just call show() method and to hide you have to call dismiss() method.

Output

Custom Dialog in Android

Total
0
Shares
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago

The default android dialog title appears as from left. For making it to center, you have to use your custom dialog style.

ajit jain
ajit jain
5 years ago

can I use recycler view inside this Dialog because i need this type of dialog box.comment image
Thanks Advance

Nilanchala Panigrahy
Nilanchala Panigrahy
5 years ago
Reply to  ajit jain

Yes you can use pretty much anything inside an Android dialog. For that you need to use DialogFragment.

Perhaps this tutorial will help http://stacktips.com/tutorials/android/android-dialog-fragment-example

StackTips
StackTips
5 years ago

Using callbacks

Previous Post

Wondershare Video Editor 3.5.0 Released

Next Post
Android Tutorials

Android CheckBox Example

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
4
0
Would love your thoughts, please comment.x
()
x