9

I have a button style with a blue background that works fine in API 22, but the same button appears in dark grey without the applied style in Android 4. This is my style:

<style name="MyApp.Plain" parent="Theme.AppCompat.NoActionBar">
      <item name="android:windowBackground">@drawable/background</item>
      <item name="android:buttonStyle">@style/MyApp.Widget.Button</item>
</style>


  <style name="MyApp.Widget.Button" parent="@android:style/Widget.Button">
      <item name="android:background">@drawable/btn_blue</item>
      <item name="android:focusable">true</item>
      <item name="android:clickable">true</item>
      <item name="android:textStyle">bold</item>
      <item name="android:textSize">14sp</item>
      <item name="android:textColor">#fff</item>
      <item name="android:gravity">center_vertical|center_horizontal</item>
   </style>

My btn_blue.xml

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

    <item android:drawable="@drawable/btn_disabled" android:state_enabled="false"/>
    <item android:drawable="@drawable/btn_pressed" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_normal_blue" android:state_enabled="true"/>

</selector>

and btn_normal_blue.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="#368ac6"
        android:startColor="#0e58a4" />

    <corners android:radius="20dp" />

</shape>

What could be the reason for this behavior and how can I fix this?

EDIT: This does not work with support v7:22.2.0, but does work with v7:21.0.3. I didn't change anything besides the dependency and changed AppCompatActivity to ActionBarActivity.

Possibly this is an Android bug.

barq
  • 3,681
  • 4
  • 26
  • 39
  • 2
    show the drawable/btn_blue contents – Lucas Crawford Aug 11 '15 at 20:06
  • Is there any chance you have separate folders for the API versions, and this style is only in one of them? – AdamMc331 Aug 11 '15 at 20:12
  • @mcAdam331: No, I only have one styles file. – barq Aug 11 '15 at 20:13
  • Do all the other attributes work? Is it clickable? Is the text style bold in 4 also? Is it *only* failing on background? – AdamMc331 Aug 11 '15 at 20:15
  • It is clickable, however for some reason the text is all caps, not bold, but same color. – barq Aug 11 '15 at 20:18
  • Added the code for btn_blue to my question – barq Aug 11 '15 at 20:20
  • Added the parent of the button style. Maybe this is causing the problems? – barq Aug 11 '15 at 20:50
  • I wonder if this Android bug has anything to do with it: https://code.google.com/p/android/issues/detail?id=160591 I am using v7:22.2.0 though – barq Aug 11 '15 at 21:28
  • This does not work with support v7:22.2.0, but does work with v7:21.0.3. I didn't change anything besides the dependency and changed AppCompatActivity to ActionBarActivity. Possibly this is an Android bug. – barq Aug 11 '15 at 22:26
  • Might not fix the problem but since your App theme inherits Theme.AppCompat.NoActionBar, you should inherit your buttonStyle from Widget.AppCompat.Button or Widget.AppCompat.Button.Colored – pmellaaho Oct 12 '15 at 09:09

4 Answers4

13

Try buttonStyle instead of android:buttonStyle, since this is AppCompat attribute pre Lollipop, so it should be without android prefix.

Michał Kisiel
  • 1,050
  • 10
  • 12
0

I have seen some layouts where the attributes are applied twice, with and without the Android prefix. Can you try this:

<style name="MyApp.Widget.Button" parent="@android:style/Widget.Button">
      <item name="android:background">@drawable/btn_blue</item>
      <item name="background">@drawable/btn_blue</item>
      <item name="android:focusable">true</item>
      <item name="android:clickable">true</item>
      <item name="android:textStyle">bold</item>
      <item name="android:textSize">14sp</item>
      <item name="android:textColor">#fff</item>
      <item name="android:gravity">center_vertical|center_horizontal</item>
</style>

I am currently trying to find where I saw this previously, and why it worked. Let me know if it did.

AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • Thanks, I tried this, but unfortunately on API 10 still grey background for the button. – barq Aug 11 '15 at 20:12
  • @barq I guess it was worth a shot. Can you post the btn_blue contents like Lucas asked? The problem may be in there, then. – AdamMc331 Aug 11 '15 at 20:13
  • @barq I found what I was thinking of, it related to the support library: http://stackoverflow.com/questions/18726865/custom-style-action-bar-not-working-in-android-4 I am leaving my answer for now, in case I am able to edit it and help you after. – AdamMc331 Aug 11 '15 at 20:18
  • Yes, I was looking at that question too. Unfortunately, this didn't work, at least not on its own. – barq Aug 11 '15 at 20:18
0

Try to use AppCompat:

    <style name="MyApp.Widget.Button" parent="Base.TextAppearance.AppCompat.Button">
     <item name="android:background">@drawable/btn_blue</item>
      ...
    </style>
dieter_h
  • 2,707
  • 1
  • 13
  • 19
0

@Michał Kisiel answer is correct "Try buttonStyle instead of android:buttonStyle, since this is AppCompat attribute pre Lollipop, so it should be without android prefix." I just want to add that if you for instance create a view programatically you should put android:buttonStyle and buttonStyle too, sample:

    <item name="android:buttonStyle">@style/OrangeButton</item>
    <item name="buttonStyle">@style/OrangeButton</item>
LinuxFelipe-COL
  • 381
  • 3
  • 7