0

I am trying to make all buttons in my app have default color using colorButtonNormal in my style.

It works nice on API 21 and above but under API 21 it only changes the background of some buttons and i dont know what is going wrong.

styles.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="colorPrimary">@color/btn_login</item>
        <item name="colorPrimaryDark">@color/bg_login</item>
        <item name="colorAccent">@color/btn_login</item>
        <item name="colorButtonNormal">@color/btn_login</item>
    </style>

</resources>

v21/styles.xml

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

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
        <item name="android:colorButtonNormal">@color/btn_login</item>
        <item name="colorPrimary">@color/btn_login</item>
        <item name="colorPrimaryDark">@color/bg_login</item>
        <item name="colorAccent">@color/btn_login</item>
    </style>



    <style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:colorPrimary">@color/btn_login</item>
        <item name="android:colorAccent">@color/btn_login</item>
        <item name="colorAccent">@color/btn_login</item>
        <item name="colorPrimary">@color/btn_login</item>
        <item name="colorPrimaryDark">@color/bg_login</item>
    </style>

    <style name="Preference" parent="Theme.AppCompat.Light">
        <item name="android:textColorPrimary">@color/black</item>
        <item name="android:colorPrimary">@color/btn_login</item>
        <item name="android:colorAccent">@color/btn_login</item>
        <item name="android:editTextColor">@color/black</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
        <item name="colorAccent">@color/btn_login</item>
    </style>



        <style name="EditTextThemeCustom" parent="Theme.AppCompat.Light">
            <!-- Customize your theme here. -->
            <item name="android:editTextColor">@color/black</item>
            <item name="android:textColor">@color/black</item>
            <item name="colorAccent">@color/btn_login</item>
        </style>


</resources>

Result:

Lollipop

and

Kitkat

Any suggestions?

ElouCapitan
  • 55
  • 1
  • 10

3 Answers3

3

Add to your styles.xml

<style name="ColoredButton" parent="Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/btn_login</item>
</style>

and then use

android:theme="@style/ColoredButton"

as one of your buttons' attributes

Vitaly Zinchenko
  • 4,871
  • 5
  • 36
  • 52
  • This doesn't work for me, however if I add other properties to ColoredButton they do work (i.e. text color) – user1282637 Mar 12 '16 at 02:06
  • 1
    `Widget.AppCopmat.Button` is a *style* not a *theme*. A theme overlay such as ` – Eugen Pechanec Dec 05 '16 at 09:51
2

The buttons you inflate will get automatically translated to AppCompatButton.

Wherever you have new Button(context) you need to use new AppCompatButton(context) instead for Material theme colors to apply.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • its ok i fixed it. i had style="?android:attr/buttonSizeSmall" something like that so when i removed it everything was fixed – ElouCapitan Oct 23 '15 at 15:22
  • With appcompat themes you'd use `?buttonStyleSmall` instead of `?android:buttonStyleSmall`. Please post it as a solution or delete the question. – Eugen Pechanec Oct 23 '15 at 15:24
0

I was getting this issue in old devices(<21) because, my activity was extending just activity when I make in it extend AppCompatActivity, in old devices also it is working perfect.

Prashanth Debbadwar
  • 1,047
  • 18
  • 33