2

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?

Tobbe
  • 1,825
  • 3
  • 21
  • 37

4 Answers4

16

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));
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
1

You can create a menu with your application icon you want to put to the right as a menu item.

res/menu/main.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/menu_item"
    android:icon="@drawable/ic_application_icon"
    android:title="@string/aplication_icon"
    app:showAsAction="always"/> 

And then inflate it using

@Override
public boolean onCreateOptionsMenu(Menu menu){

    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}
Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43
0

You have to use your own view to have the logo on the right, unless you develop your app in RTL mode, which I do not think is what you really want. You can add a RelativeLayout to your toolbar element in XML and then have the ImageView gravity set to RIGHT.

kabuto178
  • 3,129
  • 3
  • 40
  • 61
0

Instead of using the default toolbar you can use the toolbar with ta nested layout in which you can customize it according to your needs. For eg

`

<LinearLayout
    style="@style/Layout.Wrap"
    android:background="@color/sky_blue_dark"
    android:gravity="center_vertical"
    android:padding="@dimen/fivedp">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_weight="0.1">

        <ImageView
            android:id="@+id/fragment_title_bar_menu"
            style="@style/Layout.Fill"
            android:src="@drawable/ic_side_menu"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/fragment_title_bar_previous"
            style="@style/Layout.Fill"
            android:src="@drawable/ic_back_selected"
            android:visibility="gone" />
    </FrameLayout>


    <TextView
        android:id="@+id/fragment_title_bar_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:gravity="center"
        android:layout_gravity="center_vertical|center_horizontal"

        android:textColor="@color/dark_blue_title_bar"
        android:textSize="@dimen/global_title_text_size"
        android:textStyle="bold"
        android:visibility="gone" />


    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:layout_gravity="right|center_vertical"
        android:layout_weight="0.1">

        <ImageView
            android:id="@+id/fragment_title_bar_popup"
            style="@style/Layout.Fill"
            android:src="@drawable/ic_benefits_popup_icon"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/fragment_title_bar_cart"
            style="@style/Layout.Fill"
            android:src="@drawable/ic_title_bar_cart"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/fragment_title_bar_edit_profile"
            style="@style/Layout.Fill"
            android:src="@drawable/ic_title_bar_edit_profile"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/fragment_title_bar_delete_all"
            style="@style/Layout.Fill"
            android:src="@drawable/ic_delete_all"
            android:visibility="gone" />


    </FrameLayout>
</LinearLayout>

`

In this way you can do anything inside the layout that is defined inside the ToolBar

Himmat Gill
  • 341
  • 3
  • 4