6

I have an application with a split action bar loading an action menu.

I changed the actionbar for the new toolbar and replaced the split actionbar by an other toolbar used in standalone mode :

Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbarBottom);
toolbarBottom.inflateMenu(R.menu.ab_one_cam);

As specified in the documentation the action menu is pin to the right of the toolbar : enter image description here

But i would like the icons to be centered on the toolbar , like it was on the split actionbar : enter image description here

How can i make the action menu take all the available space on the toolbar ?

The toolbar is dedicated to this menu , nothing else will be added on it.

Answer

The accepted answer's link lead to a split toolbar. If like me you have very simple need this code is good enough :

public class SplitToolbar extends Toolbar {

    public SplitToolbar(Context context) {
        super(context);
    }

    public SplitToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SplitToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        if (child instanceof ActionMenuView) {
            params.width = LayoutParams.MATCH_PARENT;
        }
        super.addView(child, params);
    }
}

Credit goes to : https://gist.github.com/dodgex/7bc81fd2cbb70a8d5117

grunk
  • 14,718
  • 15
  • 67
  • 108

2 Answers2

6

In this case, Chris Banes recommends to use ActionMenuView instead of Toolbar (see the link below, reply #6). Besides that, in this link you can find a solution where the guy subclassed Toolbar in order to the split works right.

https://code.google.com/p/android/issues/detail?id=77632#c2

Hope it helps you!

Rodrigo Borges
  • 474
  • 3
  • 10
  • 1
    Using the SplitToolbar detailed in the comment of the thread did the trick : https://gist.github.com/dodgex/7bc81fd2cbb70a8d5117 – grunk Nov 05 '14 at 08:50
1

I've also been trying to find an answer to this question the past couple weeks. And the closest thing I have found takes advantage of the fact that the Toolbar is just a ViewGroup. All you need to do is create a menu_items layout that is just a LinearLayout with equally weighted menu items added. I know this isn't an ideal solution but I haven't found anyway to spread out the items while using the default menu.

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_btm"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/toolbar_bkgnd"
        android:layout_alignParentBottom="true"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar" >

        <include layout="@layout/menu_items"/>

        </android.support.v7.widget.Toolbar>
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • 1
    Can you still use inflateMenu() and setOnMenuItemClickListener() with this solution ? – grunk Oct 31 '14 at 14:29
  • Yes and no. If you are using an overflow menu you can still use the default methods for those however for the other menu items you will need to set the layouts via XML and listen for onClick events yourself. – MrEngineer13 Oct 31 '14 at 14:57
  • No reason to use a toolbar then , better create a custom view :( – grunk Oct 31 '14 at 15:30