Felipe Caldas
Felipe Caldas

Reputation: 2503

Managing Android's hierarchical styles (parenting)

Can you please try help me understanding my concepts about Android's styles and themes?

Here's my problem: I want my button to behave according to the following attribute: style="?android:attr/buttonStyleSmall"

In this scenario, the button looks like this: this is how I want

Because of my black background, I feel like having a white font color on my button to have a better contrast. I decide then to create an element in my styles.xml file to override the buttonStyleSmall style and add this white font, plus some other changes:

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

    <style name="MyTheme" parent="android:Theme.Holo.Light">
        <item name="android:buttonStyle">@style/ButtonText</item>
    </style>

    <style name="ButtonText" parent="@android:attr/buttonStyleSmall">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:gravity">center</item>
        <item name="android:paddingLeft">20dp</item>
        <item name="android:paddingRight">20dp</item>
        <item name="android:paddingTop">10dp</item>
        <item name="android:paddingBottom">10dp</item>
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">bold</item>
    </style>

</resources>

Now, I created a theme called MyTheme which I plan to apply to my whole application. That way I don't have to be declaring the buttons' styles every time I am creating a button. So I would only have the Button declaration like this:

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

Unfortunately, when I do that, this is the button I have: bad style

What happened? I did declare the parent attribute in my style, so why is the grey-like box in the button disappeared? It also does not behave as a button (i.e: when I tap it, it won't change colors). If I declare the button like this:

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/ButtonText"
        android:text="Button" />

The very same problem happens.

What am I doing wrong?

Thanks guys, Felipe

Upvotes: 0

Views: 924

Answers (1)

Bryan Herbst
Bryan Herbst

Reputation: 67189

I believe you want to inherit from @android:style/Widget.Button.Small.

This points the parent to the actual XML style definition, whereas @android:attr/buttonStyleSmall points to an attribute of buttonStyleSmall instead of the style itself.

Upvotes: 1

Related Questions