Question

I want to remove the view that is allocated for the title in a contextual action bar.

This is what I want to remove

Please note that I do not want to set the title; I want the View entirely gone. This is to allow more room so that actions can be displayed on the menu bar rather than being dropped into the overflow menu.

Also note that I do not want to set android:showAsAction="always". I want Android to decide how much space there is for the icons. I just want there to be more space by removing the blank area that is reserved for a title.

UPDATE

Things I have tried:

  • ActionMode.setCustomView(null)
  • ActionMode.setTitleOptionalHint(true)
  • ActionMode.setTitle("").

Unfortunately, none of these have worked.


UPDATE 2

It turns out my desired results are impossible to achieve in the way I was attempting. Removing the title will not allow room for more icons; Android sets a hard limit based upon the device screen size. If you are wanting to add more icons to the menu, see the answer provided by @adneal.

If you actually have a title (as shown below) and want to remove it, then you can call ActionMode.setTitle(""), as provided by @CommonsWare in the accepted answer.

Example of a menu with a title

Was it helpful?

Solution 2

Try calling setCustomView(null) on the ActionMode.

If that does not work, try setTitleOptionalHint(true) on the ActionMode.

If that does not work, try setTitle("") on the ActionMode.

OTHER TIPS

Also note that I do not want to set android:showAsAction="always"

You have to if you want more than two action buttons in the ActionBar, at least on smaller screens. The ActionBarPolicy determines how many action button to place in the ActionBar and the default amount is 2.

The only way to override that default value is to make your MenuItem "always" appear.

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTitle(null);

        for (int i = 0; i < 5; i++) {
            menu.add("Item " + (i + 1)).setIcon(android.R.drawable.sym_def_app_icon)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        }
        return true;
    }

Results

Following adneal's answer I ran into problem how to deal with the action. so I found this solution simpler: still using the xml/inflater but add

 menu.findItem(R.id.whatever).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
@Ovrride
protected void onCreate(Bundle savedInstanceState……

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.prva.xml);

I tried the solutions provided here and made some observations:

  • The view for the title is always contained in the view-hierachy (-> Layout Inspector from Android Studio) But it is in the background and will be overridden by the content.
  • setContentView adds the view provided to the layuot (in the foreground). So setContentView(null) does not remove the view for the title. (Still visible in the Layout Inspector)
  • app:showAsAction="always" is overridden when inflating the layout. You can set 3 items to be always displayed but only 2 items will be displayed.

But setting .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) programmatically after inflating the menu works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top