Search code examples
androiddrawabletoolbar

How put the logo of toolbar at right?


I did this to "inflate" the logo at the toolbar:

toolbar.setNavigationIcon(R.drawable.navigationIcon);
toolbar.getNavigationIcon().setTint(Color.BLUE);
toolbar.setLogo(R.drawable.logo);
toolbar.getLogo().methodForFloatRightHere(); 

Does there exist some simple method that doesn't use "inflate" on a view?


Solution

  • You can add custom views to a toolbar.

    The key is the android:layout_gravity="center" on the logo ImageView.

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        app:contentInsetEnd="@dimen/material_content_keyline"
        app:contentInsetStart="@dimen/material_content_keyline"
        app:navigationContentDescription="@string/abc_action_bar_up_description"
        app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha">
    
        <ImageView
            android:id="@+id/toolbar_logo"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:src="@drawable/logo"/>
    </android.support.v7.widget.Toolbar>
    

    Keyline is 72dp on phones, 80dp on tablets. This ensures any views added to toolbar (besides navigation/collapse icon and menu items) are centered in a rectangle at least 72/80dp from left and right. Increase if you have more menu items and the logo leans to the left.

    If you don't want to use XML:

    mToolbar.addView(mLogo,
        new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT, Gravity.CENTER));