Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

action bar behavior when android:targetSdkVersion is 16

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: http://i.stack.imgur.com/VsBA2.png

After android:targetSdkVersion="16": http://i.stack.imgur.com/OM6Y4.png

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

like image 698
Soham Avatar asked Oct 14 '12 14:10

Soham


1 Answers

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.

like image 136
Geobits Avatar answered Sep 21 '22 12:09

Geobits