Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button highlight in android

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)

like image 385
Ali Elgazar Avatar asked Apr 04 '13 08:04

Ali Elgazar


3 Answers

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.

like image 61
Tiago Almeida Avatar answered Oct 24 '22 22:10

Tiago Almeida


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>
like image 6
Hasham Avatar answered Oct 24 '22 23:10

Hasham


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>
like image 4
Marko Niciforovic Avatar answered Oct 24 '22 23:10

Marko Niciforovic