Take this widget layout for example (part of my whole widget layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/widget_background_dark_4x2"
android:orientation="horizontal"
android:id="@+id/widget_main"
>
I want to be able to change the background drawable used based on a users selection. For example using remote views I can update the colour of a textview by doing something like this:
remoteView.setTextColor(R.id.text_view1, Color.WHITE);
However I have been finding it difficult to the same for the background of my linear layout. I did try this:
remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable", ((BitmapDrawable) context.getResources().getDrawable(R.drawable.widget_background_dark_4x2)).getBitmap());
But I get the following error:
06-01 22:46:36.305: WARN/AppWidgetHostView(244): android.widget.RemoteViews$ActionException: view: android.widget.LinearLayout doesn't have method: setBackgroundDrawable(android.graphics.Bitmap)
<< EDIT >>
Have also tried this:
Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.widget_background_light_4x2)).getBitmap();
remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable",bitmap );
But unfortunately get the following error:
06-01 23:11:26.039: WARN/AppWidgetHostView(244): updateAppWidget couldn't find any view, using error view
06-01 23:11:26.039: WARN/AppWidgetHostView(244): android.widget.RemoteViews$ActionException: view: android.widget.LinearLayout doesn't have method: setBackgroundDrawable(android.graphics.Bitmap)
Ok, well the only solution that I could end up coming up with for this was to change the way I was displaying the background image in the LinearLayout. As you can see above I was using the android:background tag to display the image. What I ended up doing was that I removed this and then encased my entire widget layout (which was inside a linearlayout inside a relativelayout. I then just had an ImageView decalred first within the RelativeLayout and then had the rest of the widgets linearlayout sit on top of it. Then within my code I just set the imageview source via the remote view and it worked like a treat.
Not the most elegant/efficient way of coding it, but it worked!