I have the problem to customize the back arrow. I want a red arrow instead of default arrow. I've read many topics about that but it doesn't work.
So i place my styles in res/values/styles.xml. Here is my code :
<style name="AppBaseTheme" parent="Theme.Sherlock.Light">
<item name="actionBarStyle">#style/Widget.AppTheme.ActionBar</item>
<item name="homeAsUpIndicator">#drawable/btn_nav_retour</item>
</style>
I can't set "android:homeAsUpIndicator" beacause it requires api level 11 and my min sdk is 9.
Should I set the same code in res/values-v11/styles.xml ?
Thx
To work in all API's you should create style.xml in values-v11 and values-v14 folder. In values-v14 for example you should use:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/Widget.AppTheme.ActionBar</item>
<item name="android:homeAsUpIndicator">#drawable/btn_nav_retour</item>
</style>
In default values folder you should use attributes given by ActionBarSherlock, but for higher API levels you should stick with Android default ActionBar attributes and place android: in front of them. In that way it will work for higher API levels.
As a sidenote to the accepted answer:
if you don't want to duplicate your Theme for both API-ranges, you could add both items to your AppBaseTheme in values/styles.xml:
<style name="AppBaseTheme" parent="Theme.Sherlock.Light">
<item name="actionBarStyle">#style/Widget.AppTheme.ActionBar</item>
<item name="homeAsUpIndicator">#drawable/btn_nav_retour</item>
<item name="android:homeAsUpIndicator" tools:targetApi="11">#drawable/btn_nav_retour</item>
</style>
The targetApi-attribute prevents the error-message from lint. The xml-item it annotates is ignored by older Android-versions.
Related
I am using the following theme for my application:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
I would like to change the background colour. In order to do so, according to the documentation I can add the following to the theme definition:
<item name="windowBackground">#color/windowBackground</item>
Unfortunately, however, this only compiles correctly if (as suggested in the answer at No resource found that matches the given name: attr 'windowBackground') I restrict it to being applied to Android versions with API 21 or higher.
How I can I make this work on earlier versions of Android? I would ideally like to specify a background colour for Android versions down to API 16, which is my current minimum.
That's true as you said that windowBackground so you will just need to replace it with android:windowBackground that is for API 14 and higher
You need to add
<item name="android:windowBackground">#color/window_background</item>
to ../values-v21/styles.xml
// Your Problem is that You are use Theme.Appcompat then extends ActionBarActivity
if you use AppcompatActivity then use
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="android:windowBackground">#color/colorAccent</item>
</style>
I have this in my styles.xml:
<style name="UserTheme" parent="ThemeBase">
<item name="android:editTextStyle">#style/EditTextTheme</item>
</style>
Why do I have to repeat the editTextStyle line in v19/styles.xml and v21/styles.xml.
v21/styles.xml:
<style name="UserTheme" parent="ThemeBase">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:editTextStyle">#style/EditTextTheme</item>
</style>
Is there a way to just call it in the main styles.xml and have it apply everywhere so I don't have to write it multiple times?
I couldn't find any recommended solution so I i digged into AppCompat source. The way they do it is like this.
In your styles.xml
<style name="Base.V7.Theme.YourThemeName" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="Base.Theme.YourThemeName" parent="Base.V7.Theme.YourThemeName" />
<style name="Theme.YourThemeName" parent="Base.Theme.YourThemeName" >
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
In your styles-v21.xml
<style name="Base.V21.Theme.YourThemeName" parent="Base.V7.Theme.YourThemeName">
<item name="android:navigationBarColor">#color/white</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
<style name="Base.Theme.YourThemeName" parent="Base.V21.Theme.YourThemeName" />
In your styles-v22.xml
<style name="Base.V22.Theme.YourThemeName" parent="Base.V21.Theme.YourThemeName">
<item name="android:navigationBarColor">#color/black</item>
<item name="android:windowTranslucentStatus">false</item>
</style>
<style name="Base.Theme.YourThemeName" parent="Base.V22.Theme.YourThemeName" />
For every new version you extend the previous base version. If you want to override any attribute for different version just put it inside Base.VXX.Theme.YourThemeName block on your new styles-vXX.xml file.
Why do I have to repeat the editTextStyle line in v19/styles.xml and
v21/styles.xml?
If you've applied some STYLE to some attribute, Android will search styles.xml file for highest api level for which file_api_level<=Android_device_api_level and searches for STYLE in it. If it finds it would apply that STYLE to view otherwise will continue searching for the STYLE in lower api level files.
e.g. - If you have four files styles.xml(default), v19/styles.xml, v21/styles.xml, v25/styles.xml and your devices is running on api level 24. Then it'll search for STYLE in v21/styles.xml first, then v19/styles.xml and finally in styles.xml(default). Only first occurrence of the STYLE will get applied. So you can't just define only extra attributes in version-specific styles.xml file.
If you don't want to repeat common attributes here is an alternate. To declare window transitions for Android 5.0 (API level 21) and higher, you need to use some new attributes. So your base theme in res/values/styles.xml could look like this:
<resources>
<!-- base set of styles that apply to all versions -->
<style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryTextColor</item>
<item name="colorAccent">#color/secondaryColor</item>
</style>
<!-- declare the theme name that's actually applied in the manifest file -->
<style name="AppTheme" parent="BaseAppTheme" />
</resources>
Then add the version-specific styles in res/values-v21/styles.xml as follows:
<resources>
<!-- extend the base theme to add styles available only with API level 21+ -->
<style name="AppTheme" parent="BaseAppTheme">
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowEnterTransition">#android:transition/slide_right</item>
<item name="android:windowExitTransition">#android:transition/slide_left</item>
</style>
Now you can apply AppTheme in your manifest file and the system selects the styles available for each system version.
Is there a way to just call it in the main styles.xml and have it
apply everywhere so I don't have to write it multiple times?
Yes, there is a way in which you can maintain only one styles.xml file.
First of all, start using AppCompat themes. They provide backward compatibility and will work for older android versions as well.
Now define all of your styles in styles.xml(default) file and if your Android Studio is showing you some warning/error for some attribute which is supported in higher level apis:
You can suppress that warning using: tools:targetApi="SupportedAndroidVersionName"
Now Android will ignore that particular attribute if it's not supported and your whole style will work perfectly for both lower and higher api levels.
Read more about Styles and Themes here.
Hope it helps :)
Newer versions of Android have additional themes available to applications, and you might want to use these while running on those platforms while still being compatible with older versions. You can accomplish this through a custom theme that uses resource selection to switch between different parent themes, based on the platform version.
Why do I have to repeat the editTextStyle line in v19/styles.xml and
v21/styles.xml?
Because if your app is running on v21, v21/styles.xml will be loaded and if running on v19, v19/styles.xml will be loaded. In case you don't have v21/styles.xml or v19/styles.xml the app will automatically use your default values/styles.xml but you wont be able to take advantage of new features provide only for v21 or v19.
For more reference you can read Supporting Different Devices and Select a theme based on platform version.
am creating an action bar using Android API level 21, lollipop, and I've got to the point where I have to add a text button in the actionbar, I've added it successfully, but the text says: "SUBMIT" I want it to show "Submit".
Any ideas ? I've tried fiddling around with the styles with no luck here's my code:
<style name="myStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
<item name="android:textSize">17sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">false</item>
</style>
Important: This will only work on API 14+ as textAllCaps was introduced in API 14.
In your API 14+ style specify this:
<item name="android:actionMenuTextAppearance">#style/TextAppearance.MyApp.Widget.ActionBar.Menu</item>
Then define the actual font style derived from appropriate parent.
<style name="TextAppearance.MyApp.Widget.ActionBar.Menu" parent="TextAppearance.AppCompat.Widget.ActionBar.Menu">
<item name="android:textAllCaps">false</item>
</style>
If you don't use appcompat-v7 library replace AppCompat parent with Holo on API 14+ and Material on API 21+ (and prefixing the style with android:). Ie:
<style name="TextAppearance.MyApp.Widget.ActionBar.Menu" parent="android:TextAppearance.Holo.Widget.ActionBar.Menu">
<item name="android:textAllCaps">false</item>
</style>
Tested on Kitkat.
Pre-ICS
I am not able to test it but you might want to try. Please report the result.
In your pre-API 14 theme specify
<item name="actionMenuTextAppearance">#style/TextAppearance.MyApp.Widget.ActionBar.Menu</item>
Add the following to the action bar menu style:
<item name="textAllCaps">false</item>
I am using the Action Bar support library (appcompat v7), my app is set to a minimum api of 7, and a target of 21.
I have two styles files, a base one, and one targeted at devices api 11+.
When running the app on a device running KitKat, it seems that android:actionBarStyle is ignored, leaving the action bar styled as default (#style/Widget.AppCompat.ActionBar.Solid), instead of applying the given background.
But if I remove my v11 styles/comment them out, KitKat listens to the actionBarStyle attribute set in the base styles.xml file and sets my custom background without any problems.
So my question, where am I going wrong with the v11 styles?
From what I understand, according to the android docs, you are supposed to supply the additional styles for devices running 11+ using the android: prefix, but this just doesn't seem to be working for me.
Stripped down, this is my /res/values/styles.xml file:
<style name="My.Theme" parent="#style/Theme.AppCompat">
<item name="actionBarStyle">#style/ActionBar.Solid</item>
</style>
<style name="ActionBar.Solid" parent="#style/Widget.AppCompat.ActionBar.Solid">
<item name="background">#drawable/ab_solid_</item>
</style>
and this is my /res/values-v11/styles.xml file:
<style name="My.Theme" parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle">#style/ActionBar.Solid</item>
</style>
<style name="ActionBar.Solid" parent="#style/Widget.AppCompat.ActionBar.Solid">
<item name="android:background">#drawable/ab_solid_</item>
</style>
as you can see, the only difference between the two is the use of the android: prefix.
According to the official doc, with the new AppCompat-v21, you can remove all of values-v14+ Action Bar styles and use only one theme declaration, in values:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- Set AppCompat’s actionBarStyle -->
<item name="actionBarStyle">#style/MyActionBarStyle</item>
</style>
Have researched this to death and just cannot find the answer.
I have an ActionBar using AppCompat. I am supporting up to API v7 (API v8 would do). ActionBar works perfectly from API v11. API < v11 has the problem of removing an icon (and thus a feature) from the ActionBar without supplying an Overflow. The big picture is that I have an App Logo, an App Title, and 3 Icons all squidging into the same ActionBar on a low end phone. Trying to make some room!
I would be happy with 1 of 2 solutions. Either:
A method of getting an Overflow so that the functionality can be retrieved from a pull down.
(Preferred) A method of removing the icon and text from ActionBar
Below is my current XML for API 11+:
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/LiteActionBarStyle</item>
<item name="actionBarStyle">#style/LiteActionBarStyle</item>
</style>
<style name="LiteActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:displayOptions"></item>
</style>
</resources>
Specifically:
<item name="android:displayOptions"></item>
This iss the attribute that is unavailable before API v11.
This is where I am so far:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarStyle">#style/LiteActionBarStyle</item>
</style>
<style name="LiteActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"></style>
</resources>
What is the alternative for APIs prior to v11?
EDIT:
Updated Style with transparency suggestion:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarStyle">#style/LiteActionBarStyle</item>
</style>
<style name="LiteActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#android:color/transparent</item>
</style>
</resources>
Solved it!
When working with API < v11, you have to remove the "android:" element of the attribute in Style. For example:
<style name="LiteActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="displayOptions"></item>
</style>
This is how it should look in Styles with API >= v11
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/LiteActionBarStyle</item>
<item name="actionBarStyle">#style/LiteActionBarStyle</item>
</style>
<style name="LiteActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:displayOptions"></item>
</style>
Also, I was missing some Icons when I applied this on device with API < v11. I had to change the Icons to Always display:
<item
android:id="#+id/actionInvertShake"
MyApp:showAsAction="always"
android:contentDescription="#string/shakeChangeContentDescription"
android:icon="#drawable/shake"
android:title="#string/shakeOption"/>
Note that the Namespace is "MyApp" rather than "android"
This removes the Title and Icon of App from ActionBar and displays all icons from Right to Left.
Set <item name="android:icon">#android:color/transparent</item>
It will not take up any space in the ActionBar.