Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing AppWidget's background dynamically with a 9 patch drawable

There is no setBackground() method in the RemoteViews class, so I've used the following workaround:

  1. Created a FrameLayout for my app widget with an ImageView as the background view.
  2. Changed the ImageView image using setImageViewResource() method.

Unfortunately, when it comes to 9-patch drawables this method does not work. Also when an ImageView's android:src attribute points to 9-patch - it doesn't work too. Is there any way to change the AppWidget's background image programatically using a 9-patch drawable? Thanks in advance.

EDIT Settings the 9-patch as initial background in the XML:

<ImageView
    android:id="@+id/small_widget_layout_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_lime" />

When I use android:src="@drawable/background_lime" it doesn't stretch the image properly, this code works fine. And the code to change the background from the AppWidgetProvider onUpdate method:

views.setImageViewResource(R.id.small_widget_layout_bg,
                    R.drawable.backgroung_lime);

This does not stretch the image as a 9-patch.

like image 494
Egor Avatar asked Jan 18 '23 10:01

Egor


1 Answers

This answer was diagnosed in the above comments...

RemoteView doesn't allow access to View.setBackground(), so your workaround of using the android:src property of an ImageView is good, providing that the android:scaleType property is set to fitXY.

ImageView won't stretch it's foreground image unless you tell it to.

like image 75
Reuben Scratton Avatar answered Jan 19 '23 23:01

Reuben Scratton