Show possible icons in a Toolbar on Android

0

I have two ToolBars in an app, the secondary one that is shown below the main ActionBar in its totality there are 10 buttons, I want to show as much as the width allows.

The items are defined as ifRoom , which in principle are all shown in dropmenu minus one pair.

How do I force the display of the buttons on the toolbar, taking into account the width of the device and whether it is vertical or horizontal.

The load of ToolBars I do it like this.

private void initToolbars() {
    Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
    setSupportActionBar(toolbarTop);

    Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
    if (toolbarBottom != null) {

        //setSupportActionBar(mToolBar);

        toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                //process the selected tool
                switch (item.getItemId()) {
                    case R.id.action_firstcase:

                        return true;

                }
                return true;
            }
        });
        // Inflate a menu to be displayed in the toolbar

        toolbarBottom.inflateMenu(R.menu.tools);

    }
}
    
asked by Webserveis 19.06.2016 в 01:18
source

1 answer

0

To detect the orientation of the device

To get the width in dpi

DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;

Detect portrait orientation / portrait

private boolean isPortrait() {
    int orientation = this.getResources().getConfiguration().orientation;
    return orientation == Configuration.ORIENTATION_PORTRAIT;
}

Detect orientation in landscape / landscape

private boolean isLandscape() {
    int orientation = this.getResources().getConfiguration().orientation;
    return orientation == Configuration.ORIENTATION_LANDSCAPE;
}

I've been doing tests and the best that fits me in my MotoG the size of the icons more or less, 72dpi in vertical and 60 in horizontal.

long total = Math.round(dpWidth / (isPortrait() ? 72 : 60));

To get the items of the menu

Menu menuTools = toolbarBottom.getMenu();

And to force its visibility in the toolbar

if (total > menuTools.size()) total = menuTools.size();
for (int i = 0; i < total; ++i) {
    MenuItem swap = menuTools.getItem(i);
    swap.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

Not if it works with different resolutions, that is to say the values 72 and 60 say have been extracted with the error test method.

    
answered by 19.06.2016 / 01:25
source