Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying buttonStyle as theme doesn't work, but does if applied to button directly

I was following a tutorial on creating custom buttons. If I apply the style to the button directly it works, but if I apply the style to the theme using android:buttonStyle it doesn't work.

An example - Here is the style xml with the button style applied: values/styles.xml

<resources>
    <style name="MyTheme" parent="android:Theme.Light">
       <!-- the button gets styled but is no longer clickable 
            if i do it like this -->
       <item name="android:buttonStyle">@style/ButtonText</item>
    </style>

    <style name="ButtonText">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:background">@drawable/ruddy_orange_button</item>
    </style>
</resources>

drawable/ruddy_orange_button.xml:

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#70c656" />
            <stroke android:width="1dp" android:color="#53933f" />
            <corners android:radius="3dp" />
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>
   <item>
        <shape>
            <gradient android:angle="270" android:endColor="#fd4d4d" android:startColor="#f24b4b" />
            <stroke android:width="1dp" android:color="#f04a4a" />
            <corners android:radius="4dp" />
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape> 
    </item>
</selector>

However if I apply the style directly to the button it works:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/ButtonText"
    android:text="hello there" />

Any pointers on what I'm doing wrong here?

I tested this on Android 2.3.3

Source is on github.

like image 741
ed. Avatar asked Dec 08 '11 16:12

ed.


1 Answers

Try this:

<style name="ButtonText" parent="@android:style/Widget.Button">
like image 125
Warpzit Avatar answered Nov 15 '22 07:11

Warpzit