8

I have the following code for a button in the layout file for my application

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Start updating" />

By default when the button is pressed its highlight color is blue. How do I change that to the color I want?(i want it to be crimson)

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
Ali Elgazar
  • 777
  • 2
  • 12
  • 26
  • 1
    Does this help? http://stackoverflow.com/questions/3882064/how-to-change-color-of-button-in-android-when-clicked – jackgerrits Apr 04 '13 at 08:21

5 Answers5

20

A button has a set of states that can be configured like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/your_color"
      android:state_pressed="true" />
    <item android:drawable="@color/your_color"
      android:state_focused="true" />
</selector>

You can create this as a file in your res/drawables folder and then use it in your button as the background. Supose that you called that file "my_button.xml" you can then use it like this:

<Button
    android:background="@drawable/my_button"  

Or like this:

my_button.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));

Your color can be defined in the colors.xml in the res/values folder. If you don't have this file you can create it (android will recognize it). This is a good practise, but you can also replace your_color by #DC143C in the code above.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="your_color">#DC143C</color>
</resources>

Note that this color is already set for crimson.

You can also add an image for the background, replacing the "@color/your_color" by "@drawable/your_image".

For more information you can follow this link in stackoverflow.

Community
  • 1
  • 1
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • i can't seem to find the colors.xml file in res/values... i can only find the styles.xml and strings.xml files – Ali Elgazar Apr 04 '13 at 08:45
  • You have to create it. It is a good practise to do it since you have a file with all the colors you need. You can also replace your_color directly with #DC143C. The file creation is not mandatory. Only a good practise. – Tiago Almeida Apr 04 '13 at 08:47
  • 1
    As a side note you can also create a dimens.xml to hold the values of your dimensions (when you set a width or height, for instance). Those values can be acess by @dimen/my_dimen. – Tiago Almeida Apr 04 '13 at 08:48
6

Create the following xml file in your drawable folder. And set your buttons background to this drawable.

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <!-- Image display in background in select state -->
        <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>

        <!-- Image display in background in select state -->
        <item android:drawable="@drawable/button_pressed" android:state_focused="true"/>

        <!-- Default state -->
        <item android:drawable="@drawable/button"/>

    </selector>
Hasham
  • 455
  • 4
  • 11
4

What you need is selector, its a simple drawable file in which you can change colors for example of your button depending of its state, for example:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:state_focused="true"
    android:drawable="@color/white"/>
   <item 
    android:state_pressed="true"
    android:drawable="@color/white"/>
   <item android:drawable="@color/red" />
</selector>
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
1

To get you to the correct resource here is the documentation of the StateList Drawable.
check this link

You just create and define it in your res/drawable subdirectory and set it as your buttons background.

R KiranKumar
  • 825
  • 1
  • 8
  • 27
0

You can define the color on ALL of your controls like this

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Some attributes like windowNoTitle, windowActionBar, ... -->
        <item name="colorControlHighlight">@color/colorAccent</item> <!-- chose the color here -->
</style>
Yohan Dahmani
  • 1,670
  • 21
  • 33