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>
tiago
  • 22,602
  • 12
  • 72
  • 88
Jitendra
  • 1,107
  • 2
  • 12
  • 22

4 Answers4

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.

InsanityOnABun
  • 5,973
  • 5
  • 21
  • 25
  • 1
    Does this target only API 11 or APIs >= 11? – QED Aug 19 '13 at 16:50
  • 3
    @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. – InsanityOnABun Aug 19 '13 at 20:53
  • 11
    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. – QED Aug 19 '13 at 21:10
  • I couldn't add the targetApi to as shown here, but I could add it to – Fish Aug 25 '13 at 00:30
  • 1
    @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. – InsanityOnABun Aug 29 '13 at 15:02
  • oddly enough, lint does not understand that `layout-v11` is for api 11 and above only, and still requires the tools:targetApi attribute ... – njzk2 Jan 31 '14 at 15:09
  • @InsanityOnABun, What's the difference between this solution and the first answer (using `values-11` folder)? – Pacerier Nov 18 '14 at 02:20
  • 1
    @Pacerier 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. – fehbari Jan 13 '15 at 21:40
  • @FelipeBari, Hmm, the official docs always seem to recommend the first solution. – Pacerier Jan 16 '15 at 07:03
  • 1
    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. https://developer.android.com/studio/write/tool-attributes#toolstargetapi – Lifes May 10 '20 at 17:34
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.

nbro
  • 15,395
  • 32
  • 113
  • 196
hardartcore
  • 16,886
  • 12
  • 75
  • 101
  • how to use them both togather.. @style/Widget.Styled.ActionBar @style/Widget.Styled.ActionBar – Jitendra Mar 11 '13 at 13:00
  • 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 worked – Jitendra Mar 11 '13 at 13:08
  • getSuppostActionBar().setIcon(R.drawable.my_new_icon); – hardartcore Mar 11 '13 at 13:09
  • 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 color – Jitendra Mar 11 '13 at 13:37
  • can you post some image of the result you are getting because i'm not really sure if i get you right – hardartcore Mar 11 '13 at 13:45
  • 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?.. – Jitendra Mar 11 '13 at 14:43
  • What would be the possible drawbacks of just using the Sherlock theme even in more recent api levels? it just looks simpler to me – Bilthon Mar 29 '13 at 00:25
  • 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. – hardartcore Mar 29 '13 at 07:37
  • Note: folder names should be in the format `values-v14`. The answer is missing the 'v'. – Mike Ortiz Aug 26 '13 at 20:38
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.

Timothy T.
  • 602
  • 4
  • 10
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

madlymad
  • 6,367
  • 6
  • 37
  • 68
  • 2
    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 !! – Jitendra Mar 11 '13 at 14:39
  • @tobias I used the checked answer (http://stackoverflow.com/a/15339215/944070) too! – madlymad Apr 06 '13 at 16:34
  • 1
    thx for your reply! Do you know if there are drawbacks of your approach with uncheck the lint warnings? Is lint wrong here? – tobias Apr 06 '13 at 22:18
  • 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.) – madlymad Apr 07 '13 at 12:36