Search code examples
androidandroid-buttonandroid-themeandroid-styles

Why Android theme is not applying to a single button


I want to style a button in a layout using styles.xml as follows.

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="SearchButton" parent="Widget.AppCompat.Button.Colored">
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">15sp</item>
    <item name="colorButtonNormal">@color/colorPrimary</item>
</style>

And applying this style to the button using android:theme.

<Button
    android:theme="@style/SearchButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_margin="15dp"
    android:text="@string/search_button_text" />

In the preview window, this code doesn't make any changes to the default gray Material Design button.

EXTRA: I've tried using "style=" and it would work PARTIALLY, but it ignores colorButtonNormal and uses the accentColor instead.


Solution

  • The Colored Button takes the color of the colorAccent attribute from the styles.xml file. Change the color of colorAccent to the one of your choice to get the desired background color.

    colorButtonNormal is applicable only on the Buttons with default styles.

    You can try this answer.