6

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 looks like this:

enter image description here

which consists of the following background drawable:

<?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 do I programmatically change the colors of the ring and inner circle (I need to do this on the the Touch event)???

Maximus
  • 1,441
  • 14
  • 38
  • Will the second color always be the same? If so, I'd recommend creating a second drawable for the touched state. – Bryan Herbst Oct 23 '13 at 16:35
  • To be honest, I haven't decided whether to keep them the same or make the inner on different on Touch. I'm leaning towards changing both when the user Touches it. Is it difficult to do programatically? In answer to your question, the second color will not change during the lifetime of the application. – Maximus Oct 23 '13 at 16:39
  • 1
    It can be. If you look through [these questions](http://stackoverflow.com/search?q=change+shape+color+%5Bandroid%5D), you will find some methods of doing so – Bryan Herbst Oct 23 '13 at 16:41
  • see LayerDrawable documentation – pskink Oct 23 '13 at 16:46

3 Answers3

5

Late reply, but I figured out that you can access each layer in a LayerList using FindDrawableByLayerId(). Then I have access to each object and set the appropriate color!

Maximus
  • 1,441
  • 14
  • 38
0

It is possible by having different images in drawable. Consider, if you want to change the green color into black, you need to have black color circle in drawable and then try this code. This will help you..

On the button click event, use this code

public void onClick(View v) {
   if(v == ButtonName) {
     ButtonName.setImageResource(R.drawable.ImageName);
   }
}
Dinesh Kumar
  • 1,173
  • 7
  • 19
  • 30
0

You could try using a ColorStateList, it serves the purpose you are after, I think.

Mikel
  • 1,581
  • 17
  • 35
  • ColorStateList cannot be used on a drawable... http://stackoverflow.com/questions/8169257/color-state-list-not-recognized-in-shape-drawable – Maximus Oct 23 '13 at 17:12
  • your Drawable is LayerDrawable, see the docs how to access the layers – pskink Oct 23 '13 at 17:24