1

I am setting the default look of all buttons in my application as follows. When I do this, nothing happens on click of the buttons, even though I have defined all functions correctly. In fact, when I comment out the line <item name="android:buttonStyle">@style/button</item>, the button clicks work fine (but of course, they use the default android style). Application theme is also defined in the manifest: android:theme="@style/AppTheme" Can someone please tell me why this is happening? Thanks.

button_shape.xml in res/drawable:

<?xml version="1.0" encoding="UTF-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<corners
    android:radius="10dp"   />

<gradient 
    android:angle="90"
    android:startColor="#6AA4ED"
    android:endColor="#927BED"/>

<padding
    android:left="10dp"
    android:right="10dp"
    android:top="12dp"
    android:bottom="12dp" />
</shape>

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:buttonStyle">@style/button</item>
</style>

<style name="button">
    <item name="android:background">@drawable/button_shape</item>
</style>

Vedavyas Bhat
  • 2,068
  • 1
  • 21
  • 31

1 Answers1

3

Change your

<style name="button">
    <item name="android:background">@drawable/button_shape</item>
</style>

to

<style name="button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/button_shape</item>
</style>

adding the correct parent property will make you buttons click-able.

For different states of button you can use

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="@color/MyButtonDarkGray"
                android:endColor="@color/MyButtonLightGray"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/Gray" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="@color/LightGreen"
                android:startColor="@color/DarkGreen"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/Gray" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="@color/LightGreen"
                android:startColor="@color/DarkGreen"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/Gray" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

</selector>
MTahir
  • 1,173
  • 1
  • 13
  • 25
  • There isn't anything wrong with button_shape because if I use android:background="@drawable/button_shape", everything works fine, the style as well as the click. – Vedavyas Bhat Apr 04 '14 at 22:15
  • You are right, it is fine, there is a typo error. Rest I'll check again and update you – MTahir Apr 04 '14 at 22:25
  • 1
    Yes it did! Thank you very much! And what's great is, it doesn't mess up the buttons that have drawables :) – Vedavyas Bhat Apr 05 '14 at 00:12
  • There is a little formatting required in your button_shape.xml, first one is add a space before `android:shape="rectangle"`, and at the end closing `` is not displayed as it is missing code formatting. You have asked a nice question, it will definitely help others. – MTahir Apr 05 '14 at 04:41
  • Oh yes, I HAVE done that, but it got messed up while pasting here. Thanks a lot! Oh and btw, earlier you posted code for button_shape with colour states. I see that I am in need of it now. Can you post it here please? – Vedavyas Bhat Apr 05 '14 at 06:47
  • You have accepted the answer, but if you like it you can +1 as well :) – MTahir Apr 05 '14 at 08:25
  • Ah yes, I suspected it was something like this. Thanks a lot :) – Vedavyas Bhat Apr 05 '14 at 12:38