How to change the colors of shapes in Drawable?

I have the following button:

<Button android:id="@+id/Button_Collect" android:layout_width="48dp" android:layout_height="48dp" android:layout_marginBottom="16dp" android:layout_gravity="center_horizontal" android:background="@drawable/CollectButtonShape" /> 

which is as follows:

enter image description here

which consists of the following background:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:innerRadius="0dp" android:shape="ring" android:thicknessRatio="2" android:useLevel="false" > <solid android:color="@android:color/transparent" /> <stroke android:width="2dp" android:color="#FF27AE60" /> </shape> </item> <item android:top="10dp" android:left="10dp" android:right="10dp" android:bottom="10dp"> <shape android:shape="oval"> <solid android:color="#FF27AE60" /> </shape> </item> </layer-list> 

How to programmatically change the colors of the ring and the inner circle (I need to do this in the Touch event) ???

+6
source share
3 answers

Late answer, but I realized that you can access each layer in a LayerList using FindDrawableByLayerId (). Then I access each object and set the appropriate color!

+5
source

This is possible if you have different images. Think about it if you want to change the green color to black, you need to have a black circle in drawable, and then try this code. This will help you.

In a button click event use this code

 public void onClick(View v) { if(v == ButtonName) { ButtonName.setImageResource(R.drawable.ImageName); } } 
0
source

You can try using a ColorStateList , it serves your purpose, I think.

0
source

Source: https://habr.com/ru/post/956571/


All Articles