Question

How do I access items of a homescreen Widget, so I can make changes to them like setting a Button's text? In my AppWidgetProvider's onReceive() method I'm trying to set the text of a Button, which is in my homescreen widget:

@Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(MainActivity.ACTION_CHANGE_BUTTONSTATE))
        {
            String state = intent.getExtras().getString("state");
            Log.d("HomescreenWidget", "received broadcast for button state");

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.id.widgetButton);
            views.setTextViewText(R.id.widgetButton, state);
            ComponentName cn = new ComponentName(context,
                    HomescreenWidget.class);
            AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
        }
        super.onReceive(context, intent);
    }

My Widget's layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/widget_margin" >

    <Button
        android:id="@+id/playButton_Widget"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:minWidth="80dp"
        android:text="@string/playButton" />
</RelativeLayout>

I'm receiving the Broadcast, but then my Widget shows following text: "Problem loading widget".

EDIT I found following message at the catlog:

W/AppWidgetHostView(135): updateAppWidget couldn't find any view, using error view
W/AppWidgetHostView(135): android.widget.RemoteViews$ActionException: can't find view: 0x7f0a0004

0x7f0a0004 is my Widget's Button. What could be the reason, that it can't be found?

Was it helpful?

Solution 2

I've just found the solution. The whole time I was passing the wrong ID for the Button. widgetButton did not exist in the Widget's layout. It was a button from my main Activity

OTHER TIPS

To Access your view via appwidget provider you can use this remote views

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.widgetname, newName);

or via service :

public class UpdateWidgetService extends Service {

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onStart(Intent intent, int startId) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
        .getApplicationContext());

    int[] allWidgetIds = intent
        .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

    for (int widgetId : allWidgetIds) {

      RemoteViews remoteViews = new RemoteViews(this
          .getApplicationContext().getPackageName(),
          R.layout.widget_layout);

      remoteViews.setTextViewText(R.id.update,
          "Random: " + String.valueOf(number));

      appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    stopSelf();

    super.onStart(intent, startId);
  }

} 

and don't forget to add the service to the manifest

<service android:name=".UpdateWidgetService"></service> 

and then in the onUpdate (in your App Widget Provider class)

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

    ComponentName thisWidget = new ComponentName(context,
        MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    Intent intent = new Intent(context.getApplicationContext(),
        UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

    context.startService(intent);
  }

I hope this can help :)

import android.widget.RemoteViews;

//grab the layout, then set the text of the Button called R.id.Counter:

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
remoteViews.setTextViewText(R.id.Counter, "Set button text here");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top