Michał Timm
Michał Timm

Reputation: 11

Android Studio default app Widget with Configure Activity

I have problem with default Android Studio app Widget (NEW→XML→WIDGET→APP WIDGET) Shortly, default app widget providing by Android Studio works in this way:

  1. putting example widget on the main screen automatically starts Configure Activity
  2. you can add text (TextView) and then accept this by clicking Add Widget Button
  3. Configure Activity is closing and on the main screen, example widget is being shown (now on the widget you can see text you entered on the Configure activity few seconds before).

I would like to add new feature to this example. Click on widget should one more time open Configure Activity. (in default design there is no possible to open Configure Activity any more).

I tried to search it on Stack overflow but examples I found not work with that default app Widget... ;/

Upvotes: 0

Views: 719

Answers (1)

Michał Timm
Michał Timm

Reputation: 11

Finally, I found the soluton: Multiple Instances Of Widget Only Updating Last widget

In this line was my problem. PendingIntent widgetPendingIntent = PendingIntent.getActivity(context, appWidgetId, widgetIntent, 0);

Put code below in Widget class

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

    //Setup onClick
    Intent widgetIntent = new Intent(context,PaydayWidgetConfigureActivity.class);
    widgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent widgetPendingIntent = PendingIntent.getActivity(context, appWidgetId, widgetIntent, 0);

    CharSequence widgetText = PaydayWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.payday_widget);
    remoteView.setTextViewText(R.id.appwidget_text, widgetText);
    remoteView.setOnClickPendingIntent(R.id.appwidget_text, widgetPendingIntent);

    // Tell the widget manager
    appWidgetManager.updateAppWidget(appWidgetId, remoteView);

}

Upvotes: 1

Related Questions