Search code examples
androidandroid-theme

Change color of button with theme


I have trouble in changing the background color and text color of my buttons in my app.

I made a new theme to play around and try it out. My manifest looks like this:

<application
    ...
    android:theme="@style/OrangeWhite">
    <activity android:name=".MainActivity">
    ...
</application>

My theme.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="OrangeWhite" parent="android:Theme.Light">
        <item name="android:windowBackground">@color/OrangeWhite_bg_color</item>
        <item name="android:textColor">@color/OrangeWhite_wh_text</item>
        <item name="android:colorButtonNormal">@color/OrangeWhite_bg_btn</item>
    </style>

</resources>

Changing text or background color of an activity works just fine. The problem is the button. Do I need to specify on every button to use the theme OrangeWhite?...


Solution

  • If you are using AppCompat theme as parent than try below code

    If you need to change the style of a specific button, you can define a new style, inheriting one of the parent styles described above. In the example below I just changed the background and font colors:

    <style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/Red</item>
    <item name="android:colorButtonNormal">@color/Red</item>
    <item name="android:textColor">@color/White</item>
    </style>
    

    Then you just need to apply this new style on the button with:

    android:theme="@style/AppTheme.Button"