action bar behavior when android:targetSdkVersion is 16
Asked Answered
A

1

17

I am trying to implement an action bar with tabs as navigation options. To do that I select the "Tabs+Swipe" option in the "New Blank Activity" section of the "New Android App" wizard as shown below.

Tab+Swipe wizard option

Now, in the android manifest, when

android:targetSdkVersion="15"

which is the default value, the action bar looks like this on a Nexus 7/Nexus 7 emulator, this is the desired look of the app

Default behavior

Now when I change that line in the android manifest to

android:targetSdkVersion="16"

the look of the action bar changes to this

Behaviour after android:targetSdkVersion="16"

The default look remains for any version of android but 16, can anyone please explain why the look of the action bar suddenly changes when the targetSdkVersion is set to 16?

The above is reproducible in the emulator as as well as on a real nexus 7.

Full screengrabs for

Normal: https://i.sstatic.net/VsBA2.png

After android:targetSdkVersion="16": https://i.sstatic.net/OM6Y4.png

Update-10th March, 2013: Switched to the List Navigation mode (instead of Tabs) to enable move to android:targetSdkVersion=17

Anorthosite answered 14/10, 2012 at 14:41 Comment(0)
N
14

The reason it changes when setting targetSDK to 16 is because Google changed how tabs are measured in Jelly Bean. Specifically, look at this in android.internal.view.ActionBarPolicy:

public boolean hasEmbeddedTabs() {
    final int targetSdk = mContext.getApplicationInfo().targetSdkVersion;
    if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN) {
        return mContext.getResources().getBoolean(R.bool.action_bar_embed_tabs);
    }

    // The embedded tabs policy changed in Jellybean; give older apps the old policy
    // so they get what they expect.
    return mContext.getResources().getBoolean(R.bool.action_bar_embed_tabs_pre_jb);
}

However, I suspect you don't just want the reason, but a solution. Unfortunately, I can't give you a straightforward way to set it to only embedded.

I can suggest using ActionBarSherlock to make it consistent, though. The bonus to that is the ability to use actionbars on older devices. When using it(portrait), I can confirm that if you set the targetSDK to 16, it uses the stacked layout on Jelly Bean, Ice Cream Sandwich, GingerBread, and Froyo. At least you will be able to design your layout while knowing what to expect.

Keep in mind that in most cases, switching to landscape mode will embed them in the actionbar again, since there is "enough room" the way it measures.

Nutritive answered 17/10, 2012 at 21:51 Comment(6)
This is actually documented. Here: developer.android.com/reference/android/os/… : 'New ActionBar policy for embedded tabs: embedded tabs are now always stacked in the action bar when in portrait mode, regardless of the size of the screen.'Darciedarcy
Thank you, this helps, can I use ActionBar sherlock to make it embedded instead of stacked (like the pre-Jellybean default action bar behaviour) all the time?Anorthosite
@Anorthosite I don't think so, or at least not that I've seen. Since all newer versions of Android are going to be using stacked, why not just use that, so you won't have to change it later?Nutritive
@NikolayElenkov Thanks for the link. I was looking for something like that to explain it better.Nutritive
ABS is consistent on pre-ICS versions, however on JB it uses the native action bar, so you should get the same behaviour.Darciedarcy
@Nutritive the stacked version wastes a whole line of space and is unacceptable :) Let me see if I can tweak ABS to embed it by default. Thanks though.Anorthosite

© 2022 - 2024 — McMap. All rights reserved.