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?
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