Button highlight in android

30,008

Solution 1

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.

Solution 2

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>

Solution 3

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>

Solution 4

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.

Solution 5

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>
Share:
30,008
Ali Elgazar
Author by

Ali Elgazar

Not much to say. I love coding, gaming, and food!

Updated on July 09, 2022

Comments

  • Ali Elgazar
    Ali Elgazar almost 2 years

    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)

  • Ali Elgazar
    Ali Elgazar about 11 years
    i can't seem to find the colors.xml file in res/values... i can only find the styles.xml and strings.xml files
  • Tiago Almeida
    Tiago Almeida about 11 years
    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
    Tiago Almeida about 11 years
    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.