Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android app widget dynamic background color with rounded corners android 2.3

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?

like image 720
marilion91 Avatar asked Jun 27 '14 08:06

marilion91


1 Answers

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>
like image 198
marilion91 Avatar answered Jan 20 '23 03:01

marilion91