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.
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.
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With