Example usage for android.widget ActionMenuView.LayoutParams ActionMenuView.LayoutParams

List of usage examples for android.widget ActionMenuView.LayoutParams ActionMenuView.LayoutParams

Introduction

In this page you can find the example usage for android.widget ActionMenuView.LayoutParams ActionMenuView.LayoutParams.

Prototype

public LayoutParams(int width, int height) 

Source Link

Usage

From source file:com.mendhak.gpslogger.GpsMainActivity.java

public void setupEvenlyDistributedToolbar() {
    //http://stackoverflow.com/questions/26489079/evenly-spaced-menu-items-on-toolbar

    // Use Display metrics to get Screen Dimensions
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);/* w w w  .j  a va 2  s . co m*/

    // Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarBottom);

    // Add 10 spacing on either side of the toolbar
    toolbar.setContentInsetsAbsolute(10, 10);

    // Get the ChildCount of your Toolbar, this should only be 1
    int childCount = toolbar.getChildCount();
    // Get the Screen Width in pixels
    int screenWidth = metrics.widthPixels;

    // Create the Toolbar Params based on the screenWidth
    Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth,
            Toolbar.LayoutParams.WRAP_CONTENT);

    // Loop through the child Items
    for (int i = 0; i < childCount; i++) {
        // Get the item at the current index
        View childView = toolbar.getChildAt(i);
        // If its a ViewGroup
        if (childView instanceof ViewGroup) {
            // Set its layout params
            childView.setLayoutParams(toolbarParams);
            // Get the child count of this view group, and compute the item widths based on this count & screen size
            int innerChildCount = ((ViewGroup) childView).getChildCount();
            int itemWidth = (screenWidth / innerChildCount);
            // Create layout params for the ActionMenuView
            ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth,
                    Toolbar.LayoutParams.WRAP_CONTENT);
            // Loop through the children
            for (int j = 0; j < innerChildCount; j++) {
                View grandChild = ((ViewGroup) childView).getChildAt(j);
                if (grandChild instanceof ActionMenuItemView) {
                    // set the layout parameters on each View
                    grandChild.setLayoutParams(params);
                }
            }
        }
    }
}