Dan Both
Dan Both

Reputation: 228

Android: How to change the color of a button?

I'm using Eclipse ADT and I need to know how to change the default color of a form widgets button from the default gray to other color, of course using the xml.

Upvotes: 1

Views: 1049

Answers (4)

Dhiraj Choudhary
Dhiraj Choudhary

Reputation: 185

Here is answer my friend

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/grey"/>

Upvotes: 0

Ashwin S Ashok
Ashwin S Ashok

Reputation: 3663

Or you can use 9 patch images.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/patch_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/patch_normal" android:state_enabled="true"/>
<item android:drawable="@drawable/patchdisable" android:state_enabled="false"/>

Normal Pressed Disabled

And in Xml

        <Button
        android:id="@+id/button_register"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/patch"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="REGISTER"
        android:textColor="#ffffff"
        android:textStyle="bold" />

Upvotes: 2

Michael Yaworski
Michael Yaworski

Reputation: 13483

Using code in you activity class:

Button btn = (Button)findViewById(R.id.button);
btn.setBackgroundColor(Color.BLUE);
btn.setBackgroundColor(Color.rgb(0, 0, 255));
btn.setBackgroundColor(Color.parseColor("blue"));

Doing it straight from your XML:

Add this property to your Button in your XML file:

android:background="#800080"

#800080 is purple.

Upvotes: 0

Pihu
Pihu

Reputation: 1039

In your xml, you can set the background color by using android:background="#FF0000"

<Button
        android:id="@+id/loadimage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load Image" 
        android:background="#FFFF00"/>

you can set any background color of your choice...:)

Upvotes: 0

Related Questions