Zelig
Zelig

Reputation: 1808

Button's background

In an Android application, I want to change the background colour of a Button.

I tried to change the "Background" property from android:Drawable/btn_default_holo_light to #FFFF00 and I got the new color as desired. The drawback was that the shape of the Button changed as well : the original Button's margin disappeared and the coloured area got wider and higher to completely fill it's area.

How can I change only the background colour and not the Button's appearance?

Upvotes: 0

Views: 1471

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

You can set button background programatically

b= (Button) findViewById(R.id.button1);
b.setBackgroundColor(Color.RED);  

Setting background using custom drawables.

buttonbkg.xml in your drawable folder

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

normal .xml in drawable folder

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>    
<stroke android:width="3dp"
        android:color="#0FECFF" /><!-- #330000FF #ffffffff -->
<gradient 
    android:startColor="#ffffffff" 
    android:endColor="#110000FF" 
    android:angle="90"/> 

<padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
 <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 

 </shape>

pressed.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FF1A47"/>    
<stroke android:width="3dp"
        android:color="#0FECFF"/>
<padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 

In your activity_main.xml

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:background="@drawable/buttonbkg" or android:background="#0EFFFF"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Click" />

Upvotes: 4

TronicZomB
TronicZomB

Reputation: 8747

What about just android:backgroud=#FFFF00 within the button tag. I think that would keep the drawable theme therefore keep the margins, but change the color. Or is this what you had already tried?

Upvotes: 0

SimonSays
SimonSays

Reputation: 10977

You can not just change the background color like that, since the Button shape is defined in its background image. If you want a different color, then you need to create your own background. Just google for "android custom button", you will find tons of info and tutorials.

Upvotes: 0

Related Questions