0

How can I change the color of a button using the xml files? I tried using android:background="@android:color/black" but it only works for black and white as far as I can tell. Can someone explain this to me. Why not android:background="@android:color/blue"?

Code Vader
  • 739
  • 3
  • 9
  • 26
  • not all colors exist by name, you can define additional colors in colors.xml and afair use ARGB color codes directly like #ff00ff00 . But mind that assigning a background to s button disables the default multi-state background so it wont change color on press,focus... to assign all those you have to create a multistate drawable as proposed below – rupps Apr 21 '14 at 21:21

4 Answers4

0

You can do this with a selector file like so:

<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="@android:color/yellow"
            android:endColor="@android:color/yellow"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@android:color/grey" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <gradient
            android:endColor="@android:color/orange"
            android:startColor="@android:color/orange"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@android:color/grey" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item>        
    <shape>
        <gradient
            android:endColor="@android:color/blue"
            android:startColor="@android:color/blue"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@android:color/grey" />
        <corner
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

Name the file custom_button.xml for example, then in the actual button:

background="@drawable/custom_button"

Referenced from : Standard Android Button with a different color

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
0

Change color of button i have 2 answers for u :-

  1. you can use android:background="#0000ff" for blue and for color codes u can visit http://html-color-codes.info/

  2. i would suggest rather to use xml drawables to accomplish dat you will create a onclick state drawable and normal state drawable then you will use them in one drawable. This way you can give different color during onclick.

Thx.

Ahmad
  • 437
  • 1
  • 4
  • 12
0

you have to define drawable attibute for the items (for some reason it is required when it comes to background definitions), so:

  <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/red"/> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
    <item android:drawable="@color/black"/> <!-- default -->
</selector>

Also note that drawable attribute doesn't accept raw color values, so you have to define the colors as resources. Create colors.xml file at res/values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <color name="black">#000</color>
     <color name="blue">#00f</color>
     <color name="red">#f00</color>
</resources>

You can also take a look on this

Aboullaite
  • 394
  • 1
  • 3
  • 17
0

you can create a file under res/values/ folder, and name it color.xml . and then add your colors in that file like this :

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue" >#0000ff</color>
<color name="red" >#ff0000</color>
<color name="green">#00ff00</color>
</resources>

And then in your xml layout , you can specify the color you want like this :

android:background="@color/blue"

instead of

android:background="@android:color/blue"

NOTE : you can see this tutorial as an advanced step for you to use selectors on your buttons and views.

Houcine
  • 24,001
  • 13
  • 56
  • 83