0

I have an android widget with a 9-patch background image.

Instead of using the main layout "android:background" property I use a RelativeLayout with an ImageView set behind other elements. The result is ok.

Now I want the user to be able to skin the widget, and especially choose the background color (using a color picker).

I'll find a way to build the new 9-patch at runtime but I firstly wanted to be sure that I was able to change the background 9-patch.

So I built manually a different 9-patch PNG and I compiled it using aapt.exe then put it on the phone. Then I tried to change the 9-patch using RemoteViews.setImageViewUri(). The result is that the new PNG is applied but not as a 9-patch, it is stretched instead.

Below the details:

The widget layout:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <ImageView
    android:id="@+id/widgetBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
        android:src="@drawable/rounded_black_70"/>

    <LinearLayout
    ...
  </LinearLayout>

</RelativeLayout>

Pictures describing the 9-patch files and the results: image

The line of code I use to set the new 9-patch:

remoteViews.setImageViewUri(R.id.widgetBackground, Uri.parse("/sdcard/DCIM/test.9.png"));

My question: has anyone of you ever managed to apply a new (generated at run time) 9-patch image using a remoteview?

What I have tried already:

  • I tried to apply the new image using a drawable and RemoteViews.setImageViewBitmap or setImageViewResource

  • I read the following post which says "I need to store it as a compiled nine patch. So that I can directly assign it to an ImageView (using its URI). As it's the only way to set a runtime generated nine patch on RemoteViews". It seems that this guy knew how to do it. Saving run-time generated NinePatch on external memory

  • so I learnt about compiled and not-compiled 9-patch.

  • I tried with files named ".9.png" or just ".png"

Thank you

1 Answers1

0

Let me answer my own question.

  • I don't think one can change the 9-patch via RemoteViews. I think that some people tried to inherit RemoteViews, thinking that they would be able to override the display functions and have access to all the existing UI functions in android instead of just the small set in RemoteViews. cf "Remember that when you send updates over, you are effectively redrawing the app widget, so you can use your layout files to affect changes that you cannot do via methods on RemoteViews." and also "I am just guessing. Can you try to extend RemoteViews and override the apply function" (I can't post links but you'll find the pages with google) -> but this solution didn't work for me (the apply function is never triggered)
  • My final solution was: use whitish 9-patch images and then use setColorFilter: remoteview.setInt(R.id.your_background, "setColorFilter", your_color); You really have a good control on the result with setColorFilter, see http://chiuki.github.io/android-shaders-filters/#/
  • I think that there was an alternate solution using shapes instead of 9-patches