6

I made a custom 9-patch images for my button's background. Buttons are in drawable-hdpi and drawable-mdpi folder. I created custom selector file for my button states.

selector file login_button.xml:

<?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:state_pressed="true" android:drawable="@drawable/login_button_down" />

    <!-- Image display in background in select state -->
    <item android:state_focused="true" android:drawable="@drawable/login_button_down" />

    <!-- Default state --> 
    <item android:drawable="@drawable/login_button" />
</selector>

Then I made a custom styles.xml file for the button style:

<style name="login_button_style" parent="@android:style/Widget.Button">
        <item name="android:gravity">center_vertical|center_horizontal</item>
        <item name="android:textColor">#FF000000</item>
        <item name="android:shadowColor">#FFFFFFFF</item>
        <item name="android:shadowDx">0</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">0.2</item>
        <item name="android:textSize">13dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@drawable/login_button</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
    </style>

Then applied this style to my theme file in themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customTheme" parent="@android:style/Theme.NoTitleBar" >
        <item name="android:editTextStyle">@style/login_edittext_style</item>
        <item name="android:buttonStyle">@style/login_button_style</item>
        <item name="android:textViewStyle">@style/login_textview_style</item>
    </style>
</resources>

And finally added button itself to the layout file:

<Button 
   android:text="@string/login_text" 
   android:id="@+id/buttonSignIn" 
   android:layout_width="130dp" 
   android:layout_height="wrap_content">
</Button>

But if I click the button, then background image is not changed. Code is ok and all compiles nicely. I know that I have same image for two different states, but it doesn't work even for one state in emulator. Can anyone point me where is the problem?

EDIT:

Obviously normal state is working, because it gets it's image from selector xml file. Now i'm wondering why the other states are not...

evilone
  • 22,410
  • 7
  • 80
  • 107

1 Answers1

5

I thought maybe is something to do with naming, so I named button state images with different name than login_button, because selector xml file has the same name. And I edited my selector xml file also.

Selector xml file:

<?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:state_pressed="true" android:drawable="@drawable/login_btn_down" />

    <!-- Image display in background in select state -->
    <item android:state_focused="true" android:drawable="@drawable/login_btn_down" />

    <!-- Default state --> 
    <item android:drawable="@drawable/login_btn" />    
</selector>
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
evilone
  • 22,410
  • 7
  • 80
  • 107