android:actionBarStyle requires API level 11
Asked Answered
L

4

60

While using the ActionBarSherlock in xml at:

<item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>

I got this error:

android:actionBarStyle requires API level 11 (current min is 8) error

I'm using it for back porting my app with actionbar to 2.2 devices.

How to use them both together:

 <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
 <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
Lapwing answered 11/3, 2013 at 12:55 Comment(2)
actionbarsherlock.com/theming.htmlCasual
I prefer the solution of using <code>tools:targetApi="11"</code>Seamount
L
77

You have to use only :

<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> 

as you can get the error, you have android:actionBarStyle available at API level 11.


If you want to be able to style your ActionBar to look the same in all API levels, you need to create different folders for the selected API level and create new style.xml/themes.xml files in these folders.

For example:

- res
  -- values
     -- styles.xml
     -- themes.xml // API LEVEL 8+
 -- values-v11
     -- styles.xml
     -- themes.xml // API LEVEL 11+
 -- values-v14
     -- styles.xml
     -- themes.xml // API LEVEL 14+

The second thing which I can think of is be careful which themes are you including to your current one at different API Levels.

For example, for API level 8: you will use @style/Theme.Sherlock.Light.DarkActionBar and you will have to use only actionBarStyle. While styling the action bar for API level 14+, you won't need actionBarStyle , because you probably will set Holo.Light theme as parent for your current one, so in this situation you will have to use android:actionBarStyle.

Linebreeding answered 11/3, 2013 at 12:59 Comment(9)
how to use them both togather.. <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>Lapwing
hi can you answer that how can i change the icon of actionbar different from the default icon...? I tried some code but it did not workedLapwing
getSuppostActionBar().setIcon(R.drawable.my_new_icon);Linebreeding
ya i tried it but I can not set the background of the actionbar icon to transparent and my activity have custom background so it is having the background as app color rather the actionbar colorLapwing
can you post some image of the result you are getting because i'm not really sure if i get you rightLinebreeding
I solved error... and according to my question it is most suitable answer thank you... any way can you provide me your contact mail in case of query?..Lapwing
What would be the possible drawbacks of just using the Sherlock theme even in more recent api levels? it just looks simpler to meCelanese
In some cases it is recommended, because using for example SearchView and Holo Theme will crash, because of some properties which are not available in Holo theme. For me, I am using Sherlock Theme even in Api level 11+ and just style the other views to look like Holo.Linebreeding
Note: folder names should be in the format values-v14. The answer is missing the 'v'.Belva
Z
147

Another option is to use the tools:targetApi attribute, which requires the tools namespace. This acts in a similar fashion to the @TargetApi annotation you can use in java files.

<resources xmlns:tools="http://schemas.android.com/tools">

<style name="MyThemes.MyTheme">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="android:actionBarStyle" tools:targetApi="11">@style/Widget.Styled.ActionBar</item>
</style>

</resources>

Note the xmlns:tools="http://schemas.android.com/tools" in the <resources> tag, as it is required.

Zollverein answered 13/8, 2013 at 18:30 Comment(10)
Does this target only API 11 or APIs >= 11?Puklich
@psoft APIs >= 11. It effectively tells the Lint checker to treat that item as if the min API was 11, instead of whatever you have it set in your manifest.Zollverein
I don't know why your answer doesn't have more up votes, it's so much easier than making a bunch of different files.Puklich
I couldn't add the targetApi to <item> as shown here, but I could add it to <style>, so I created several styles with the same name in the same file, each with a different targetApi. It seems to be working so far (although it took ages to work the first time).Maighdlin
@psoft Probably because the question is over 2 years old :) Fish: That's odd. I've been using it without issue on many different devices and APIs.Zollverein
oddly enough, lint does not understand that layout-v11 is for api 11 and above only, and still requires the tools:targetApi attribute ...Iago
@InsanityOnABun, What's the difference between this solution and the first answer (using values-11 folder)?Uppercut
@Uppercut The difference is you don't need to create a values-11 folder and maintain multiple styles.xml files. I personally prefer this solution since it's much simpler.Veldaveleda
@FelipeBari, Hmm, the official docs always seem to recommend the first solution.Uppercut
This only stops the warning. It does not stop a runtime error from occurring if you actually use the item on an API that does not support it. developer.android.com/studio/write/…Simony
L
77

You have to use only :

<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> 

as you can get the error, you have android:actionBarStyle available at API level 11.


If you want to be able to style your ActionBar to look the same in all API levels, you need to create different folders for the selected API level and create new style.xml/themes.xml files in these folders.

For example:

- res
  -- values
     -- styles.xml
     -- themes.xml // API LEVEL 8+
 -- values-v11
     -- styles.xml
     -- themes.xml // API LEVEL 11+
 -- values-v14
     -- styles.xml
     -- themes.xml // API LEVEL 14+

The second thing which I can think of is be careful which themes are you including to your current one at different API Levels.

For example, for API level 8: you will use @style/Theme.Sherlock.Light.DarkActionBar and you will have to use only actionBarStyle. While styling the action bar for API level 14+, you won't need actionBarStyle , because you probably will set Holo.Light theme as parent for your current one, so in this situation you will have to use android:actionBarStyle.

Linebreeding answered 11/3, 2013 at 12:59 Comment(9)
how to use them both togather.. <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>Lapwing
hi can you answer that how can i change the icon of actionbar different from the default icon...? I tried some code but it did not workedLapwing
getSuppostActionBar().setIcon(R.drawable.my_new_icon);Linebreeding
ya i tried it but I can not set the background of the actionbar icon to transparent and my activity have custom background so it is having the background as app color rather the actionbar colorLapwing
can you post some image of the result you are getting because i'm not really sure if i get you rightLinebreeding
I solved error... and according to my question it is most suitable answer thank you... any way can you provide me your contact mail in case of query?..Lapwing
What would be the possible drawbacks of just using the Sherlock theme even in more recent api levels? it just looks simpler to meCelanese
In some cases it is recommended, because using for example SearchView and Holo Theme will crash, because of some properties which are not available in Holo theme. For me, I am using Sherlock Theme even in Api level 11+ and just style the other views to look like Holo.Linebreeding
Note: folder names should be in the format values-v14. The answer is missing the 'v'.Belva
B
7

You can just select errors in Eclipse and press on your key "Delete". Then just run the project and it will work.

You have delete theses errors each time you modify your XML.

Bicapsular answered 11/3, 2013 at 13:51 Comment(1)
Funny, but it really works for quick hacks, where you do not want to mess with the project.Cyton
N
7

It depends what SDK Version you want to target:

Target devises lower than 11:

At your AndroidManifest.xml use:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="x" android:maxSdkVersion="10"/>

x anything between 8-10 (depends on your code)

At your style use:

<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>

Target any device:

At your AndroidManifest.xml use:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />

16 used at ActionBarSherlock example can be any greater or equal to 11 (depends on your code)

At your style use both:

<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> 
<item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>

the 1st one is for ActionBarSherlock theme and the 2nd is for using the same theme in android devices that already support ActionBar

Edit: To clear Lint warnings (red underlining in XML file that may show up):

Clear Lint Warnings

Nordin answered 11/3, 2013 at 14:27 Comment(4)
ya that is right but using both in same file is not possible because it will give you error of min required sdk version of 11 !!Lapwing
@tobias I used the checked answer (https://mcmap.net/q/162905/-android-actionbarstyle-requires-api-level-11) too!Nordin
thx for your reply! Do you know if there are drawbacks of your approach with uncheck the lint warnings? Is lint wrong here?Fractious
I guess that Lint warns (looking at its preferences some warnings are generated as errors) that the code is not recognised in the OS version, but as long as Android xml is designed to ignore unrecognized statements (IF possible) it still works fine. So lint is right, the underlined code is unknown, but still can be ignored on runtime! :) // that's my approach why it works based to the fact that xml unknown tags ignored (I have read it somewhere sry don't remember the src, or all the cases that functionallity is applied.)Nordin

© 2022 - 2024 — McMap. All rights reserved.