0

I have a widget layout with a drawable background:

<RelativeLayout
    android:id="@+id/basic_widget_layout"
    android:background="@drawable/basic_widget_style_2"
    android:clickable="true" >

@drawable/basic_widget_style_2 is a layer-list of two shapes.

I also have a configuration activity that displays a preview of my widget for real-time tweaking:

<include android:id="@+id/preview_include"
         layout="@layout/basic_widget_layout" />

I'm trying to programmatically tweak @drawable/basic_widget_style_2 through my configuration activity and see the real-time results in @+id/preview_include.

In my configuration activity I have the specific appWidgetId that launched me, so my question would be - Is there a possible way to obtain the @drawable/basic_widget_style_2 that belongs to the widget that launched my activity?

Additionally, how do I invalidate the <include>-ed layout background drawable? It seems that no matter what I do, the layout remains static (tried using invalidate(), refreshDrawable() and more tricks I've seen).

prgDevelop
  • 1,557
  • 2
  • 15
  • 26

1 Answers1

0

I cannot understand what exactly you want to change in the background drawable.

In order to get the drawable shape of your View you need to assign an id to the item including the shape and then do this:

//change the color to the specific needed
LayerDrawable preview_include = (LayerDrawable)(this.findViewByID(R.id.preview_include)).getBackground();
final GradientDrawable preview_include_shape = (GradientDrawable)   include.findDrawableByLayerId(R.id.rectange);

If you want to change color of the shape for example do this:

        preview_include_shape.setColor(getResources().getColor(getResId("white", R.color.class)));

The XML Drawable with the shape might be the following:

<?xml version="1.0" encoding="utf-8"?>

<!-- Up 2dp Shadow -->
<item >
    <shape  android:shape="rectangle">

        <solid android:color="@color/shadow" />
         <corners 
          android:radius="3dp"
             />
    </shape>
</item>

<!-- A rectange -->
<item android:top="3px" android:id="@+id/rectange">
    <shape android:shape="rectangle">
        <corners 
            android:topLeftRadius="3dp"
            android:topRightRadius="3dp"
            android:bottomLeftRadius="3dp"
            android:bottomRightRadius="3dp"/>
            <solid android:color="@color/black"
                    />
    </shape>
</item>

Pontios
  • 2,377
  • 27
  • 32