Button style working in Android 5 but not Android 4
Asked Answered
B

4

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.

Bourque answered 11/8, 2015 at 19:48 Comment(10)
show the drawable/btn_blue contentsKelleykelli
Is there any chance you have separate folders for the API versions, and this style is only in one of them?Ophthalmia
@mcAdam331: No, I only have one styles file.Bourque
Do all the other attributes work? Is it clickable? Is the text style bold in 4 also? Is it only failing on background?Ophthalmia
It is clickable, however for some reason the text is all caps, not bold, but same color.Bourque
Added the code for btn_blue to my questionBourque
Added the parent of the button style. Maybe this is causing the problems?Bourque
I wonder if this Android bug has anything to do with it: code.google.com/p/android/issues/detail?id=160591 I am using v7:22.2.0 thoughBourque
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.Bourque
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.ColoredTrident
O
13

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

Oink answered 14/3, 2016 at 12:52 Comment(1)
This should be accepted as the answer, fixed my problem! Thanks.Ardeen
O
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.

Ophthalmia answered 11/8, 2015 at 20:9 Comment(4)
Thanks, I tried this, but unfortunately on API 10 still grey background for the button.Bourque
@Bourque I guess it was worth a shot. Can you post the btn_blue contents like Lucas asked? The problem may be in there, then.Ophthalmia
@Bourque I found what I was thinking of, it related to the support library: #18727365 I am leaving my answer for now, in case I am able to edit it and help you after.Ophthalmia
Yes, I was looking at that question too. Unfortunately, this didn't work, at least not on its own.Bourque
A
0

Try to use AppCompat:

    <style name="MyApp.Widget.Button" parent="Base.TextAppearance.AppCompat.Button">
     <item name="android:background">@drawable/btn_blue</item>
      ...
    </style>
Ambros answered 11/8, 2015 at 20:13 Comment(3)
Thanks, but this did not work either, unfortunately.Bourque
Did you set the style to the button?Ambros
No, it is the general button style for the app so there should be no need. It works without doing that in Android 5.Bourque
K
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>
Kanzu answered 6/3, 2017 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.