androidlayer-list

Modifying layer list programmatically


In my Android app, there is a layer-list background to a linearlayout. Following is the code.

   <LinearLayout android:id="@+id/ut2" 
   android:layout_width="70dp" 
   android:layout_height="70dp"
   android:orientation="vertical"
   android:background="@drawable/circle_tapped"
   android:gravity="center"
   >  
 <ImageView 
 android:id="@+id/tv3"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:src="@drawable/time"
 android:scaleType="centerInside"
 android:onClick="selectit"
 android:clickable="true"
 />
 </LinearLayout>

circle_tapped.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/pat1">
<shape android:innerRadius="0dp" android:shape="ring" android:thicknessRatio="2"     android:useLevel="false" >
<solid android:color="@android:color/transparent" />

<stroke
    android:width="5dp"
    android:color="#FF27AE60" />
</shape>
</item>
<item android:id="@+id/pat2" android:top="10dp" android:left="10dp" android:right="10dp" android:bottom="10dp">
<shape android:shape="oval">
    <solid android:color="#FF27AE60" />
</shape>
</item>
</layer-list>

Basically this code draws a ring around the image. I want to change the color of the ring programmatically when the linearlayout is clicked. How to do that?


Solution

  • This solves it.

     View v = findViewById(R.id.layout_id);
    
    LayerDrawable bgDrawable = (LayerDrawable)v.getBackground();
    final GradientDrawable shape = (GradientDrawable)   bgDrawable.findDrawableByLayerId(R.id.shape_id);
    shape.setColor(----);