5

I'm developing a Android application, using the ActionBarSherlock library. In one activity, I use a tabbed navigation in combination with a collapsed ActionBar (action items at the bottom).

In this picture you can see the Activity in its current state: The tabs are being pushed in a second row.

Current ActionBar


In the following picture you can see the Activity the way I want it to be: The tabs should be in the top row, not in a second row. I already read the ActionBar and ActionBarSherlock documentation, but found no way to force this behaviour.

The wished ActionBar layout

This is the current code, used to create the 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?

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
damaxxed
  • 2,464
  • 2
  • 16
  • 17
  • 1
    Your title says "prevent" but your question suggests that, in fact, you want the `Tabs` to collapse into the `ActionBar`. – adneal Jul 14 '12 at 14:11

1 Answers1

5

There is a reflection 'hack' to do this. I take no credit for the solution, which I found in this StackOverflow question replicate ActionBar Tab(s) with custom view.

//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);
    }
}

See also this blog post: http://sparetimedev.blogspot.co.uk/2012/11/forcing-embedded-tabs-in-actionbar.html

Community
  • 1
  • 1
Andy
  • 983
  • 7
  • 13