3

Is it possible to launch a dialog on widget click or do I have to launch a Activity on widget click then a dialog?

Blake
  • 307
  • 1
  • 5
  • 12

2 Answers2

11

I am sure you have already solved that, but I'll write here anyway, in case other like me land on this answer.

To launch a Dialog by clicking a Widget you can define an activity that is styled like a dialog and launch it when the widget gets clicked.

First of all add the activity in the manifest, let's call it DialogWidgetActivity

<activity
    android:name=".DialogWidgetActivity"
    android:theme="@android:style/Theme.Dialog"
    ...
/>

Then in your WidgetProvider, let's call it MyWidgetProvider bind the click of the widget with the launch of the activity

public class MyWidgetProvider extends AppWidgetProvider {

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

    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];

        // Create an Intent to launch the activity-dialog
        Intent intent = new Intent(context, DialogWidgetActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        // Get the layout for the App Widget and attach an on-click listener
        // to it
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget);
        views.setOnClickPendingIntent(R.id.widget_container, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current app
        // widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

}
}

Note that here we have bind the launch of the activity to the click on the widget_container element of the dialog. You can easily achieve this by assigning the id widget_container, android:id="@+id/widget_container", to the root element of the widget layout.

You can then write your activity-dialog and its layout like you would do for a normal full screen activity.

My sources were this answer and the Android documentation.

Hope I helped :)

Community
  • 1
  • 1
mokagio
  • 16,391
  • 3
  • 51
  • 58
-2

You can launch AlertDialogon click of a widget.

Update:

Add the following code for onclick listener.

    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    dialog.setMessage("Message");
    dialog.show();
Tushar Vengurlekar
  • 7,649
  • 8
  • 33
  • 48
  • I'm attempting, but failing, do you have code, or a link to an example? Thanks. I've been messing around with pending intents set in the `onUpdate` method of the `AppWidgetProvider`, I've got the basic `AlertDialog` ready to go should my clicks have some sort of effect. – Timbermar Mar 27 '11 at 03:09
  • Please Check updated answer. You can modify per your requirements. – Tushar Vengurlekar Mar 28 '11 at 09:12
  • `android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application`. So nope. Voted down. – machei Jul 12 '15 at 21:12