If an Android ActionBar is split into a top and a bottom portion using android:uiOptions="splitActionBarWhenNarrow"
in the Manifext.xml
,
is there a way to force some of the actions to be displayed in the top portion instead of having them all at the bottom?

- 2,550
- 2
- 22
- 28
-
Looks like the answer is "no" and was answered at http://stackoverflow.com/questions/8571754/android-split-action-bar-with-action-items-items-on-top-and-bottom – Marcus Wolschon Jan 31 '12 at 09:49
3 Answers
There's no standard way of doing this. That said, the Action Bar custom view will appear in the upper bar, so you can just use that. You will lose some of the extras (toasts on long press), so you'll have to implement them yourself. That said, of you're using ActionBarSherlock, all the layouts and styles for a normal button are there, so you can just use those.

- 8,881
- 4
- 37
- 42
Yes! You can continue to use android:uiOptions="splitActionBarWhenNarrow"
in your Manifest.xml.
You just also need to set:
// set the actionbar to use the custom view
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//set the custom view to use
getActionBar().setCustomView(R.layout.custom_action_bar_top);
Where R.layout.custom_action_bar_top
would be a view that has all the buttons you wish to appear in the top action bar.
All the menu items that you want at the bottom, should be added as usual in the onCreateOptionsMenu
method of your activity.

- 2,283
- 2
- 20
- 38
-
1Hi @immersion, this makes sense but have you tried it? Does it actually work that way? – ericn Feb 19 '14 at 02:18
-
2Hi @fuzzybee, yeah when I needed to do this, I only needed a search bar up the top so I used: `SearchView searchView = new SearchView(context); getSupportActionBar().setCustomView(searchView, new LayoutParams(Gravity.RIGHT));` If you want to see what it looks like, check out [SwishPix](https://play.google.com/store/apps/details?id=com.immersion.swishpix.free). The screenshot of the list of albums is an example of what it might look like. – Dylan Watson Feb 19 '14 at 02:41
-
With this solution you would have to reimplement the icon and title manually as well, so it won't be an easy drop-in solution. – Apr 05 '14 at 00:34
-
Unless something has changed since I last looked, using the `setCustomView` approach doesn't remove the Title or 'Up/Home' Icon. You can just use them as usual. – Dylan Watson Apr 05 '14 at 04:20
-
Well when was the last time you checked? It appears to remove them on Android 4.~ – brainmurphy1 Jul 11 '14 at 23:33
-
I just checked in my app, compiled for 4.2, running on my android 4.3 Samsung S3. The title and home button are not removed and the SearchView appears at the top right. – Dylan Watson Jul 14 '14 at 02:51
-
1In my app compiled for 4.4 and running on 4.4, this makes my app icon and activity title disappear. But adding DISPLAY_SHOW_HOME and DISPLAY_SHOW_TITLE to setDisplayOptions brings them back. – arlomedia Aug 31 '14 at 18:29
I would like to second Dylan Watson's solution, but with one improvement.
For those who would like to keep the title, and not replace the entire actionBar with their new View, they use getActionBar().setDisplayShowCustomEnabled(true)
, rather than getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
, as the latter will cause the new view to be the only view displayed in the actionbar. My code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(this);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setCustomView(searchView, new ActionBar.LayoutParams(Gravity.RIGHT));
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

- 1,082
- 1
- 19
- 24
-
Confirmed, this enables the custom view without disabling the home button and title as in Dylan's answer. If the title still doesn't appear, it might be covered by the custom view; this answer provides some layout options: http://stackoverflow.com/a/16517395/462162 – arlomedia Aug 31 '14 at 18:31