lopez.mikhael
lopez.mikhael

Reputation: 10061

How to replace title by logo in Android Toolbar?

I want to replace the title in my Toolbar by a logo like Twitter App.

I think we can replace this programmatically like that :

mToolbar = (Toolbar) findViewById(R.id.app_toolbar);
mToolbar.setLogo(R.drawable.ic_logo);

But I want to replace it directly in my XML toolBar declaration, but I can't see the property app:logo or android:logo.

<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/app_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/MyCustomToolBarTheme" />

Please help me, thank you.

Upvotes: 0

Views: 2625

Answers (2)

dagalpin
dagalpin

Reputation: 1307

A toolbar contains a ViewGroup that you can populate with whatever views you wish --- you can add an ImageView there if you want a logo. Example:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/ic_logo"/>
</android.support.v7.widget.Toolbar>

If you manually attach it to AppCompatActivity or ActionBar activity, it "becomes" an ActionBar, and then has to be manipulated using those code functions.

Upvotes: 2

hidro
hidro

Reputation: 12521

If you check source code of Toolbar and Toolbar's custom attributes, there are only a few attributes you can use to customize Toolbar, but logo is not one of them:

  • titleTextAppearance
  • subtitleTextAppearance
  • navigationIcon
  • ...

Toolbar.setLogo(resId) seems to be the only option at the moment.

Upvotes: 5

Related Questions