Home » Android » Android Core » Modal dialog (popup) from Android widget example

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Modal dialog (popup) from Android widget example

In this example we are going to see  how you can create an Android widget in the home screen that can open a pop up dialogue box. As you know, Android Widgets are small applications that can basically do two things. Launch a new Activity when pressed, or display information that get updated at a decided time interval.

1. Introduction

Widgets make use of RemoteViews to display their user interface. RemoteViews can be executed by another process with the same permissions as the original application. This way the Widget runs with the permissions of its defining application. This is form the official Android Documentation : ” RemoteView is a class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.”

The user interface of a  Widget is defined by an BroadcastReceiver. This BroadcastReceiver inflates the layout into the RemoteViews of the Widget.  Then RemoteViews is delivered to Android, which updates the user interface at the home screen application.

Widgets have limited functionality and styles compared to Activities. So you may need to thing some workarounds to do more complex stuff, like we want to do. And so to launch a pop up dialogue from our widget here’s what we do: When the widget is clicked we simply launch a new Activity with android:theme="@android:style/Theme.Dialog" property set in the configuration of the Activity in AndroidManifest.xml.

To sum up the basic steps, we are going to:

  • Create an Android Widget.
  • Create an Intent that when sent to the BroadcastReceiver it marks the launching of the new Activity.
  • Register a ClickListener to the widget. Thus when the widget is pressed the above Intent will be sent to the BroadcastReceiver.
  • When this Intent is received, a new Activity that will look like a pop up dialog box, will be lauched.

Let’s see how it’s done.

2. Create a new Android Widget

For this tutorial, we will use the following tools in a Windows 64-bit platform:

  1. JDK 1.7
  2. Eclipse 4.3 Kepler
  3. Android SKD 4.3

2.1 Create a new Android Project

Open Eclipse IDE and go to File -> New -> Project -> Android -> Android Application Project.


You have to specify the Application Name, the Project Name and the Package name in the appropriate text fields and then click Next.


In the next window un check “Create activity” option as we will create the PopUpActivity later.


Click “Finish”.

2.2 Define a custom background shape for the widget

Go to the package explorer and open a /res/drawable-* folders.


Right Click on one of the folders (I’ve picked /res/drawble-hdpi) -> New -> Other -> Android -> Android XML File


From the “Root Element” list select shape and the give the file the name custom_shape.xml:


Now, open that file and paste the following code , describing the layout of a simple shape.


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

        android:startColor="#333" />

        android:topRightRadius="2dp" />

        android:color="#333" />


2.3 Define a simple layout for the widget

Navigate to res/layout folder on the Package Explorer. Right Click on the folder -> New -> Other -> Android -> Android XML Layout File. Create the file  with name widget_layout.


And paste the following code:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/custom_shape" >

        android:text="Press Me"
        android:textColor="#FFF" >



2.4 Create the AppWidgetProvider XML description file.

Go to File -> New -> Other -> Android -> Android XML File. From the “Resource Type” list select “AppWidget Provider” and name the file widget_provider.xml.


You will find the file under the newly created /res/xml folder:


Open the file and paste the following code, which is basically meta data for the AppWidgetProvider.


<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="120dp" >


2.5 Create the AppWidgetProvider class

Now create a new class that extends AppWidgetProvider, under com.javacodegeeks.android.androidwidgetdialog package. This will be the BroadcastReceiver of the widget.


package com.javacodegeeks.android.androidwidgetdialog;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class AndroidWidget extends AppWidgetProvider {

	private static final String SHOW_POPUP_DIALOG_ACTION = "com.javacodegeeks.android.showpopupdialog";

	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {	

		ComponentName thisWidget = new ComponentName(context,

		// Obtain all instances of our widget
		// Remember that you can have as many instances of the same widget as you want on the home screen 

		int[] allWidgetInstancesIds = appWidgetManager.getAppWidgetIds(thisWidget);
		for (int widgetId : allWidgetInstancesIds) {

			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),

			// Create an intent that when received will launch the PopUpActivity
			Intent intent = new Intent(context, AndroidWidget.class);

			PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
					0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

			// Set up the onClickListener of the widget
			// Now, when the widget is pressed the pendingIntent will be sent

			remoteViews.setOnClickPendingIntent(R.id.myText, pendingIntent);

			appWidgetManager.updateAppWidget(widgetId, remoteViews);


		super.onUpdate(context, appWidgetManager, appWidgetIds);


	public void onReceive(final Context context, Intent intent) {

		// If the intent is the one that we've defined to launch the pop up dialog
		// then create and launch the PopUpActivity
		if (intent.getAction().equals(SHOW_POPUP_DIALOG_ACTION)) {

			Intent popUpIntent = new Intent(context, PopUpActivity.class);


		super.onReceive(context, intent);



As you can see we use a PendingIntent instance. A PendingIntent is a description of an Intent and target action to perform with it. When you give a PendingIntent to another application, you are granting it the right to perform the operation you have specified with the same permissions and identity. And thus, you should be careful about how you use the PendingIntent. In this example we’ve obtained the PendingIntent instance calling getBroadcast method. This method will retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().

Now the flow of the execution goes like this:

  1. As defined, the onUpdate method is executed only once, when the widget is installed on the home screen. You can set up a time interval to execute the onUpdate method by defining the android:updatePeriodMillis property on the widget_provider.xml file. So when, onUpdate method is executed, first we retrieve all the instance ids of the widget that the user has installed on the home screen.
  2. Then we create a new Intent with action com.javacodegeeks.android.showpopupdialog. Retrieve a new broadcasting PendingIntent
  3. Register an OnClickPendingIntent listener to the RemoteView of each widget.
  4. Now when the user clicks on the Widget, the Intent with action com.javacodegeeks.android.showpopupdialog will be broadcast. This is the Intent created in line 32 of the above code (used also to create the PendingIntent instance).
  5. The Intent will be received and the onReceive will be executed. If the Intent received has com.javacodegeeks.android.showpopupdialog action the PopUpActivity class will be launched.

Now let’s see the code of the PopUpActivity.

3. Create the PopUpActivity

PopUpActivity is a simple Activity class that will be launched as a Dialogue box.

Go to the Package Explorer and Right click on the com.javacodegeeks.android.androidwidgetdialog package -> New -> Other -> Android -> Android Activity.


Set the name to “PopUpActivity”. As you can see the layout file of this activity will be automatically created with name “activity_pop_up.xml”.


To set the layout of the activity open /res/layout/activity_pop_up.xml file and paste the following code.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:textSize="13sp" />

        android:text="Close" />


This is the code of the PopUpActivity class.


package com.javacodegeeks.android.androidwidgetdialog;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class PopUpActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {

		String dialogText = "Dialog Box : " + (new Random().nextInt(100));
		TextView txt = (TextView) findViewById(R.id.mytxt);

		Button dismissbutton = (Button) findViewById(R.id.closBtn);
		dismissbutton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {


Nothing spacial in the above code. We simple set the value of the mytxt TextView. And we register a ClickListener for the dismiss button. When the button is pressed the PopUpActivity will be terminated.

4. AndroidManifest.xml Configuration

This is a very important step of the tutorial. Open AndroidManifest.xml file and paste the following code:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >

        android:targetSdkVersion="18" />

        android:theme="@style/AppTheme" >

            android:label="Android Custom Widget" >
                <action android:name="com.javacodegeeks.android.showpopupdialog" />
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

                android:resource="@xml/widget_provider" />

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


As you can see we’ve set up a receiver, that registers our code>AppWidgetProvider for our application, that can receive broadcasts

  1. <action android:name=”com.javacodegeeks.android.showpopupdialog” />
  2. <action android:name=”android.appwidget.action.APPWIDGET_UPDATE” />

The first one is the Intent that we’ve registered ourselves. The second one is an Intent send by Android. We also provide the meta data to the the AppWidgetProvider, pointing to the widget_provider.xml resource file we’ve created earlier.

The important bit is in line 34, where we define the theme of the application as android:theme="@android:style/Theme.Dialog". This will create an activity with a pop up dialogue theme

Let’s see how it looks like on the emulator.

5. Run the Apllication

When your run the application and the emulator launches go to the Apps button on the home screen :


The tap on “Widgets”. Drag and drop your Widget to the home screen


Here it is:


Now when you click on the Widget:


That’s it.

Download the Eclipse Project

This was an Android example on how to create Modal dialog (popup) from Android widget. Download the Eclipse Project of this tutorial: AndroidWidgetDialog.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....




  1. It’s a little weird seeing Eclair this day in age

  2. This was very useful. Thanks!

  3. How can i get a pop up window on my custom dialog?? On clicking a bottom in dialog…. i get my popup in main activity not the dialog!!

Leave a Reply

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


Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns