Tab Crash in ActionBar

I am developing an Android application using the library ActionBarSherlock. In one action, I use the navigation tab in combination with the folded one ActionBar(action items below).

In this picture, you can see Activityin your current state : tabs are inserted in the second row.

Current ActionBar


In the following figure, you can see the Activityway I want: the tabs should be on the top line, and not on the second line. I already read the documentation ActionBarand ActionBarSherlock, but did not find a way to force this behavior.

The wished ActionBar layout

This is the current code used to create ActionBar.

public class AdminActivity extends SherlockFragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab itemsTab = actionBar.newTab().setText(R.string.label_tab_items);
    ActionBar.Tab usersTab = actionBar.newTab().setText(R.string.label_tab_users);

    actionBar.addTab(itemsTab);
    actionBar.addTab(usersTab);
}

Any ideas?

+5
source share
1

"" . , StackOverflow ActionBar .

//pre-ICS
if (actionBarSherlock instanceof ActionBarImpl) {
    enableEmbeddedTabs(actionBarSherlock);

//ICS and forward
} else if (actionBarSherlock instanceof ActionBarWrapper) {
    try {
        Field actionBarField = actionBarSherlock.getClass().getDeclaredField("mActionBar");
        actionBarField.setAccessible(true);
        enableEmbeddedTabs(actionBarField.get(actionBarSherlock));
    } catch (Exception e) {
        Log.e(TAG, "Error enabling embedded tabs", e);
    }
} 

//helper method
private void enableEmbeddedTabs(Object actionBar) {
    try {
        Method setHasEmbeddedTabsMethod = actionBar.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
        setHasEmbeddedTabsMethod.setAccessible(true);
        setHasEmbeddedTabsMethod.invoke(actionBar, true);
    } catch (Exception e) {
        Log.e(TAG, "Error marking actionbar embedded", e);
    }
}

: http://sparetimedev.blogspot.co.uk/2012/11/forcing-embedded-tabs-in-actionbar.html

+5

All Articles