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.
Try buttonStyle instead of android:buttonStyle, since this is AppCompat attribute pre Lollipop, so it should be without android prefix.
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.
Try to use AppCompat:
<style name="MyApp.Widget.Button" parent="Base.TextAppearance.AppCompat.Button">
<item name="android:background">#drawable/btn_blue</item>
...
</style>
#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>
Related
button style rounded corners not working on android earlier than level 21
Note that I'm applying this style on the whole project
drawable edittext.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="15dip"
android:bottomLeftRadius="15dip"
android:topLeftRadius="15dip"
android:topRightRadius="15dip"/>
</shape>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/colorPrimary</item>
<item name="android:buttonStyle">#style/App_ButtonStyle</item>
<item name="editTextStyle">#style/App_EditTextStyle</item>
<item name="android:spinnerStyle">#style/App_SpinnerStyle</item>
</style>
<style name="App_EditTextStyle" parent="#android:style/Widget.EditText">
<item name="android:background">#drawable/edittext</item>
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:padding">5dip</item>
<item name="android:gravity">right</item>
</style>
<style name="App_SpinnerStyle" parent="#android:style/Widget.Spinner">
<item name="android:background">#drawable/edittext</item>
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:padding">5dip</item>
<item name="android:gravity">right</item>
</style>
<style name="App_ButtonStyle" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/edittext</item>
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:padding">10dip</item>
<item name="android:gravity">center</item>
<item name="android:width">150dip</item>
</style>
also spinner style not applied,
This screenshot show how it on android API level 21+
and the below picture shows how it be on earlier than level 21
So any idea what is wrong with this style
Note that Widget.AppCompat.Button didn't solve the problem
Instead of creating another styles.
use button background android:background="#drawable/edittext"
this worked for my in every API level.
My checkbox styles are being applied on android v5+, but not on v4.x
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColor">#color/ui_text</item>
<item name="android:windowAnimationStyle">#null</item>
<item name="android:textStyle">bold</item>
<item name="android:alertDialogStyle">#style/AppDlg</item>
<item name="android:radioButtonStyle">#style/RadioButtonStyle</item>
<item name="android:checkboxStyle">#style/CheckBoxStyle</item>
<item name="android:editTextStyle">#style/EditTextStyle</item>
</style>
<style name="RadioButtonStyle" parent="CheckBoxStyle">
<item name="android:textColor">#color/ui_text</item>
<item name="android:textStyle">bold</item>
<item name="android:button">#drawable/radiobutton</item>
</style>
<style name="CheckBoxStyle" parent="#android:style/Widget.Holo.CompoundButton.CheckBox">
<item name="android:textColor">#color/ui_text</item>
<item name="android:paddingLeft">8dip</item>
<item name="android:drawablePadding">12dip</item>
<item name="android:textStyle">bold</item>
<item name="android:button">#drawable/checkbox</item>
<item name="android:buttonTint">#color/primary</item>
<item name="android:layout_width">#dimen/checkbox_size</item>
<item name="android:layout_height">#dimen/checkbox_size</item>
</style>
...
I am compiling against, and targeting SDK 25, with a minimum SDK of 15. I am using support-v4:25.3.0, appcompat-v7:25.3.0, design:25.3.0
Android Studio gives me an error in styles.xml about buttonTint requiring a minimum API level of 21, but even with this line removed I get the same behavior (checkboxes being styled on 5.x, but not on 4.x)
It seems I needed to add the following lines to styles.xml
<item name="checkboxStyle">#style/CheckBoxStyle</item>
<item name="radioButtonStyle">#style/RadioButtonStyle</item>
to compliment the lines that were already there...
<item name="android:radioButtonStyle">#style/RadioButtonStyle</item>
<item name="android:checkboxStyle">#style/CheckBoxStyle</item>
!
in my app i added back button on ActionBar but this is not visible properly because of dark theme of ActionBar. both ActionBar and back Button are black in color so back button is not visible.
i added this code in values.xml file
<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">#drawable/up</item>
</style>
and set.
minSdkVersion="14"
but it's not showing custom back buttom.
In the res/values/styles.xml file put some custom style:
<style name="AppThemeWithBackButton" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBar</item>
<item name="android:actionMenuTextColor">#color/white</item>
<item name="android:homeAsUpIndicator">#drawable/actionbar_back_button</item>
</style>
<style name="ActionBar" parent="android:Widget.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="android:displayOptions">useLogo|homeAsUp|showTitle</item>
<item name="android:titleTextStyle">#style/TitleText</item>
</style>
<style name="TitleText" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
<item name="android:textAllCaps">false</item>
</style>
and use drawable/actionbar_back_button.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="16dp"
android:right="16dp"
android:drawable="#drawable/back_icon" />
</layer-list>
and use drawable/actionbar_background.xml layout.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/action_bar_color" />
</shape>
Above all code is regarding of customizing actinobar button and background. You can modify it as you want. Hope it will help you. Enjoy!!!
I am trying to change the color of an item when it pressed. I used the actionBarItemBackground item in the action bar style, but nothing happens.. The color not changed...
Here my code:
<style name="ActionBarStyle" parent="#style/Widget.AppCompat.ActionBar.Solid">
<item name="android:background">#color/actionbar_background</item>
<item name="titleTextStyle">#style/ActionBarStyle.Title</item>
<item name="actionBarItemBackground">#drawable/action_bar_item_selector</item>
<item name="android:actionBarItemBackground" tools:ignore="NewApi">#drawable/action_bar_item_selector</item>
</style>
And the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_item" android:state_focused="true"/>
<item android:drawable="#drawable/pressed_item" android:state_pressed="true"/>
<item android:drawable="#android:color/transparent"/>
</selector>
And the drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#FF0000"/>
</shape>
What is worng? Someone?
actionBarItemBackground isn't inherit of Widget.ActionBar. You should be placing it in your root theme, wherever you apply your ActionBarStyle, but not in your ActionBarStyle. So, something like:
<style name="Your.Theme" parent="#style/Theme.AppCompat">
<item name="actionBarStyle">#style/ActionBarStyle</item>
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
<item name="actionBarItemBackground">#drawable/action_bar_item_selector</item>
<item name="android:actionBarItemBackground" tools:ignore="NewApi">#drawable/action_bar_item_selector</item>
</style>
I'm trying to change a TextView style based on its state.
My styles.xml contains:
<style name="text_normal_ops">
<item name="android:gravity">left</item>
<item name="android:textColor">#color/text_usual_color</item>
<item name="android:textStyle">bold</item>
</style>
<style name="text_normal_ops_pressed">
<item name="android:gravity">left</item>
<item name="android:textColor">#color/text_pressed</item>
<item name="android:textStyle">bold</item>
</style>
My selector (text_ops.xml)is defined as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" style="#style/text_normal_ops_pressed" />
<item android:state_focused="true" style="#style/text_normal_ops_pressed" />
<item android:state_selected="true" style="#style/text_normal_ops_pressed" />
<item style="#style/text_normal_ops"/>
</selector>
But when I apply this to my textview (style="#drawable/text_ops") it does not work.
Any tips?
Thanks
In android as per my knowledge there is only two state-list 1. Color State List Resource 2. StateListDrawable. If you are using style in it then please recheck the doc
Please check below link for more info
http://developer.android.com/guide/topics/resources/color-list-resource.html
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
The problem is the line style="#drawable/text_ops" this should be style="#style/text_ops".
I haven't tried using a selector for styles but it would be cool if it works that way.