How to create Dialog activity in Android?

A dialog is a small window which prompts the user to enter additional information. We have discussed about how to create a dialog in one of our articles Dialog.  In this tutorial we would be learning how to create an activity dialog in android. As shown in the below figure, an Activity Dialog is like an Activity floating over another Activity(parent activity).

Dialog activity in Android Example - 1

You can’t access the parent activity until the dialog activity is destroyed. When the dialog activity closes, the parent activity gets its focus.

To create a floating activity, you need to apply dialog Theme while defining the activity entry in the AndroidManifest.xml file. Follow the below code to know about how to add the Theme to the Manifest file,

android:theme="@android:style/Theme.Dialog" >

If you use ‘Theme.Dialog’ for your activity, it would be shown as a floating Dialog. You can create buttons like “OK”, “CANCEL” etc for this dialog through the activity’s layout.

Step 1: Set up Android working environment

We have already discussed about how to set up android working environment in one of our articles, please go through the post Environment. if you are not familiar with the android environment.

Step 2: Create an Android application

To know how to develop a simple android application, please refer to the post Create a simple app.

Step 3: Create the required layouts

Here I would be using main.xml. I would just add a simple button to this layout to launch our floating Activity. The code of the main.xml is as below,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
android:orientation="vertical" >

android:text="Start a floating Activity" />


The layout xml for the dialog activity is “floatingactivity.xml”. This layout file just has a textview that displays a text when the activity is started. The code of the floatingactivity.xml is as below,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical" >

android:text="I am a floating Activity" />


Step 3: Create the required activities

Here I would be using the launcher activity DialogActivityDemoActivity which is created automatically when you create your sample application, as a parent activity to launch our floating activity ( Our floating Activity is launched when a button named “Start a floating activity” in the parent activity is clicked. The code of the parent activity¬† is as below,


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DialogParentActivity extends Activity {
Button btnsecond_activity;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

btnsecond_activity.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(), DialogActivity.class);

As you can see in the above code, we are launching a new dialog activity on click of a button in the parent activity. The has the below code,


import android.os.Bundle;
import android.view.Window;

public class DialogActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub


I have made the dialog activity with no Title.

Step 4: Define the activities in AndroidManifest.xml

Your launcher activity is declared automatically by the android system. To know more about AndroidManifest.xml, please refer the post Manifest. The content of your manifest file is as below,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />

android:label="@string/app_name" >
android:name=".DialogParentActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
android:theme="@android:style/Theme.Dialog" >


You can notice the Theme.Dialog is being applied to the activity “DialogActivity”.

Step 5: Run your application

The parent Activity fires up as below,

Dialog activity in Android Example - 2 - parentdialog

Click on the button “Start a floating Activity”. The dialog activity opens up as below,

Dialog activity in Android Example - 3

Even though this is an Activity for all the purposes, it can behave as a dialog inside your UI, partially covering the parent activity. Click the window outside the Floating Dialog, the dialog would be closed and the parent activity gets focus. If you want to dismiss the dialog activity on click of a button, then you need to add a button to the dialog activities layout and you can add listeners for its click events. If you don’t want the dialog activity to be closed when you click the surface outside the dialog, add the below line to your dialog activities code,


If you add the above line , you can handle the click events of the dismiss buttons in case if you add them to your layouts.

This is how you can create an Activity as a dialog in Android. “Theme.Dialog” can be used to create the look and feel of the dialogs, with all the features of a full-blown Activity. Hope this helped you. If you have any queries, please post it in comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!