android app widget dynamic background color with rounded corners android 2.3
Asked Answered
V

1

9

I'm facing a problem with my implementation of changing the widget background color dynamically in android 2.3.

I used this approach for my implementation: http://oceanuz.wordpress.com/2013/07/11/widget-remoteviews-round-corners-background-and-colors/

So I have an ImageView in my WidgetLayout:

 <ImageView 
    android:id="@+id/widget_background_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/widget_bg_shape" />

This is how the widget_bg_shape looks like:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid
        android:color="#496fb3"/>
    <corners
        android:radius="3dp" />
</shape>

And my code changing the Background Color based on user preferences:

private static void setBackgroundColor(Context pContext, int pWidgetID,
        RemoteViews remoteViews) {
    float[] color = { 218, 59 / 100F, 70 / 100F };
    int transparency = 192;
    SharedPreferences settings = pContext.getSharedPreferences("Widget"
            + pWidgetID, Context.MODE_PRIVATE);
    Color.colorToHSV(settings.getInt("color", Color.HSVToColor(color)),
            color);
    transparency = settings.getInt("transparency", transparency);
    remoteViews.setInt(R.id.widget_background_image, "setColorFilter",
            Color.HSVToColor(color));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        remoteViews.setInt(R.id.widget_background_image, "setImageAlpha",
                transparency);
    } else {
        remoteViews.setInt(R.id.widget_background_image, "setAlpha",
                transparency);
    }

So on the newer android versions this works fine, but on Android 2.3 (testing on Samsung S+ with android 2.3.6, had also some user feedback from other devices) the background is always completely transparent.

I found out, that either the setColorFilter or the setAlpha call on the remoteView caused the Image to be fully transparent or not there.

According to the Article postet above and the stackoverflow question referenced (Rounded corners on dynamicly set ImageView in widget?) this should work on android 2.2 and obove. But in my case it does not.

Can anybody help me with this?

Vitriolic answered 27/6, 2014 at 8:37 Comment(1)
Your question is the answer i was looking for, thanks mate where ever ur :)Mirza
V
11

Better late than never:

I fixed the issue by removing the default color from the background drawable shape for android 2.x

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="3dp" />
</shape>
Vitriolic answered 15/1, 2015 at 15:54 Comment(2)
same code as in the question but one xml drawable without the solid attribute as default ressource and the one in the question for sdk version 14 and aboveVitriolic
Bro, I wish I could give you many up-votes. I searched a lot, finally got this.Taciturn

© 2022 - 2024 — McMap. All rights reserved.