Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access drawable resources (color) in java

I want to access color resource defined as drawable resource and desire to toggle the background color in JAVA, basically background of a button was changed using below mentioned drawable XML. I tried accessing button and modify color attribut but this changed the shape of button to normal square shape. I want to keep shape as defined in drawable XML and change background color manually.

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

<item android:state_pressed="true">
    <shape android:shape="rectangle">
        <solid android:color="#EAEAEA" />

        <corners android:bottomLeftRadius="8dip"
             android:bottomRightRadius="1dip"
             android:topLeftRadius="1dip" 
             android:topRightRadius="8dip" />
    </shape>
    </item>

<item><shape android:shape="rectangle">
        <solid android:color="#EAEA00" />

        <corners android:bottomLeftRadius="8dip" 
            android:bottomRightRadius="1dip" 
            android:topLeftRadius="1dip" 
            android:topRightRadius="8dip" />
    </shape>
     </item>

like image 340
RN55 Avatar asked Oct 20 '12 11:10

RN55


1 Answers

You have 2 possibilities:

  • myButton.setBackgroundColor(Color.CHOOSE_ONE);
  • myButton.setBackgroundResource(R.color.youCustomColor);

If you want to set the color from an hexadecimal value just use the static method of the Color class:

myButton.setBackgroundColor(Color.parseColor("#RRGGBB"));
//http://developer.android.com/reference/android/graphics/Color.html#parseColor%28java.lang.String%29
like image 167
flawyte Avatar answered Oct 11 '22 15:10

flawyte