I’m creating a widget where the user in the configuration activity can select from a variety of graphics to be used in a button (ImageView (or ImageButton) with StateListDrawable). The selected graphic will be used in the widget layout when the widget is added. The layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/button"
android:layout_width="72dip"
android:layout_height="72dip"
android:background="@null"
android:clickable="true">
</ImageView>
</LinearLayout>
My question is, how do I set the selected backgroundResource in the ImageButton shown in the widget based on the users choice in the configuration activity?
I’ll answer my own question here. It seems like it’s not possible to set a StateListDrawable in RemoteViews via code. The solution for me was to use XML-layouts which has references to unique selectors (I actually made a generator, so that I could generate them easily). This is tedious when working with a large number of graphic elements, but seems to be the only option.