JustCurious
JustCurious

Reputation: 1858

How to launch a dialog box from a widget?

I couldnt find anywhere a case that a widget launches a dialog box when it is clicked. Do you have any ideas?

I have this code

public void onUpdate(Context c, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    AppWidgetManager mgr = AppWidgetManager.getInstance(c);
     for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
    RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
    Intent fireDialog = new Intent(c,Execute.class);

    Toast test=Toast.makeText(c.getApplicationContext(),"onUpdate",Toast.LENGTH_LONG);
    test.show();
    PendingIntent pendingIntent = PendingIntent.getActivity(c, appWidgetId, fireDialog, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.Button, pendingIntent);
    Log.w(LOG_TAG,"Called");
    mgr.updateAppWidget(appWidgetId, views);
     }
}

Upvotes: 4

Views: 6224

Answers (3)

ron
ron

Reputation: 9396

You can create an AlertDialog customized to your needs. Then you can alertDialog.show() from an onClickListener callback of an other widget (say Button) to show the dialog.

Upvotes: 0

ixc
ixc

Reputation: 21

make a activity with dialog theme, and call it

<activity android:name=".DailyPillDialog" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog">

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007533

I am assuming here that by "widget" you mean "app widget", the interactive elements an application can add to the user's home screen.

An app widget cannot display a Dialog, as that can only be done by an Activity. Your app widget can start up an Activity, though, via startActivity(). And, you can theme your activity to look like a dialog, by adding android:theme="@android:style/Theme.Dialog" to your element.

Upvotes: 10

Related Questions