Coding Android Activity dialogs the easy way

  • Written by  Clive

Getting active: Using an Activity as an Alert Dialog

Activity dialog iconYou can use an activity as a custom dialog. Simply create the activity.

There’s no need to use any Dialog classes.

Here’s how you do it.

Creating the Custom Activity dialog

Create an activity as you normally would.

Create the activity’s layout file. This is what mine looks like:

Android Activity as Alert Dialog

Activity Alert Dialog

This is a normal activity

It’s all in the Theme: Edit the manifest

Open up the AndroidManifest.xml file.

Edit your activity element to include the theme attribute:

Theme.Dialog for activity alert dialog

Set the theme for the activity to make it look like a dialog

The theme floats the activity window and puts a frame around its contents.

Which theme you choose depends on your target devices.

  • Theme.Dialog – default for API 10 and lower
  • Theme.Holo.Dialog – default for API 11 and newer

Displaying the Activity Alert Dialog

Using Theme.Dialog turns the activity into a dialog

Showing the Custom Activity dialog

Just start the activity to show the dialog.

Here’s how I did it:

Start the Activity as Alert Dialog

Create an Intent using this constructor, Intent(context, the activity that we want to start).

Call startActivity(), passing the intent. This will start the activity and display the “dialog”.

You can then capture the user’s input as you would with any activity.

It’s really that simple!

I hope you found this tutorial helpful.

Here are the links to all the tutorials in this series on Android Dialogs;

This project was created using Android Studio. You can download the project files here  File Download icon

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.