1

I have buttons and i want to set background xml for them, and when a user press any button i want to change its background , i work like this

<Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="#FFFFFF"
                android:gravity="center"
                android:background="@drawable/button_selector"
                android:text="@string/b_cancel" />

button_selector.xml

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

    <item android:drawable="@drawable/button_bg" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/button_bg_hover" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_bg_hover" android:state_pressed="false" android:state_selected="true"/>

</selector>

button_bg.xml

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

    <padding android:top="10dip"
        android:bottom="10dip"/>



    <!-- Gradient Bg for listrow -->
    <gradient
        android:useLevel="false"
        android:angle="270"
        android:centerColor="#000000"
        android:endColor="#FFFFFF"
        android:startColor="#808080" />
</shape>

button_bg_hover.xml

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

    <padding
        android:bottom="10dip"
        android:top="10dip" />

    <!-- Gradient Bg for Button -->
    <gradient
        android:angle="270"
        android:centerColor="#000000"
        android:endColor="#FF0000"
        android:startColor="#808080" />

</animation-list>

it works good as a background but when i press the button it becomes all white, inspire of my button_bg_hover , why pelase? what is the correct ?

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
user user
  • 2,122
  • 6
  • 19
  • 24

1 Answers1

1

it could be the misplaced -tag, change the line

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
  ..... 
</animation-list>

to

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  .....
</shape>
Tobrun
  • 18,291
  • 10
  • 66
  • 81
  • i put it at end and still the white background appears – user user Feb 09 '13 at 13:41
  • maybe the animation-list tag is misplaced and it should be shape – Tobrun Feb 09 '13 at 13:43
  • i miss to put `shape` , i will accept yoru ansewr, and please is this a good way to put padding in the background ? because when i remove it the buttons become so small – user user Feb 09 '13 at 13:47
  • depends what you mean by background, I usually declare my padding on the button itself. But i'm not sure if there is a difference – Tobrun Feb 09 '13 at 13:50