Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompat v21 Toolbar changing logo size

I am migrating to the new Toolbar feature in appcompat v21 from the previous action bar. I still want to keep the logo on the top left part of the actionbar (toolbar). For doing I added in my layout the support toolbar and I created a new thene for it.

    app:theme="@style/NewToolBarStyle"

I am adding the log programmatically as there is some logic in the app for this.

            actionBar.setLogo(R.drawable.myicon);

Referring to my new style (empty for the moment):

<style name="NewToolBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</style>

However the result is showing an image the is too big for what I am looking for, and I am wondering how to reduce the size of the icon.

Is there any way (style, layout or programming) that I can reduce the logo size?

like image 848
Trebia Project. Avatar asked Nov 25 '14 20:11

Trebia Project.


1 Answers

There is no logo icon in material design : http://www.google.com/design/spec/layout/structure.html#, so I suppose this is not well tested scenerio - or simply (broken) by design. You can add ImageView as a child widget of your toolbar and use it to show any image. It will show on the right of all the other internal widgets - like spinner - but list navigation mode is also deprecated.

If you insist on having logo then my workaround for this is to make sure toolbar is of fixed height - this takes care of wrong icon height. Even after that you will have to set setAdjustViewBounds to true on toolbars internal logo ImageView - otherwise it will create large left and right padding.

This is how my toolbar looks like (height set to ?attr/actionBarSize):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>

reference it inside your activity layout using:

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

dont change layout_height in include.

The second step is to setAdjustViewBounds(true) on logo icon:

    Drawable logo = getDrawable(iconRes);
    toolbar.setLogo(logo);
    for (int i = 0; i < toolbar.getChildCount(); i++) {
      View child = toolbar.getChildAt(i);
      if (child != null)
        if (child.getClass() == ImageView.class) {
          ImageView iv2 = (ImageView) child;
          if ( iv2.getDrawable() == logo ) {
            iv2.setAdjustViewBounds(true);
          }
        }
    }
like image 181
marcinj Avatar answered Sep 21 '22 19:09

marcinj