Martin Forget
Martin Forget

Reputation: 23

Android: How to update widget text on creation

I have seen several questions regarding how to update widgets but nothing is helping me with my issue.

I created a widget that has a textview that I want to dynamically update upon widget creation. I have a configuration activity that is called when adding the widget on the screen. I store the value in the sharedpreferences and retreive it onUpdate. The problem is that the widget doesn't get updated. Any idea how it can be done? I do not want to update the textview after a click on the widget, just get the correct text to be displayed on creation.

public class AndroidWidget extends AppWidgetProvider {

@Override

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

    ComponentName thisWidget = new ComponentName(context,
            AndroidWidget.class);

    int[] allWidgetInstancesIds = appWidgetManager
            .getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetInstancesIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);

        // Create an intent that when received will launch the PopUpActivity
        Intent intent = new Intent(context, AndroidWidget.class);
        intent.setAction(SHOW_POPUP_DIALOG_ACTION);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

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

        // Set up the onClickListener of the widget

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

        SharedPreferences prefs = context.getSharedPreferences(
                String.valueOf(widgetId), Context.MODE_PRIVATE);

        remoteViews.setTextViewText(R.id.myText,
                prefs.getString("storedtext", null));

        appWidgetManager.updateAppWidget(widgetId, remoteViews);

    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);

}

This actually updates the textview but only after clicking or creating another widget.

Upvotes: 2

Views: 986

Answers (1)

Orcun Sevsay
Orcun Sevsay

Reputation: 1473

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

        //set widget id
        ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        for (int i = 0; i < appWidgetIds.length; i++) {
            Intent intentService = new Intent(context, WidgetService.class);

            intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);

            Intent clickIntent = new Intent(context, MainActivity.class);
            PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            widget.setTextViewText(R.id.currency, " Dollar");

            appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);

    }

This code works for me. You may try this.

Upvotes: 3

Related Questions