Set Toolbar logo in XML
Asked Answered
L

7

13

Is it possible to set Toolbar logo in xml? I have tried this so far:

1.

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ...
    android:logo="@drawable/my_logo"
    />

2.

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ...
    app:logo="@drawable/my_logo"
    />

EDIT

I'm using toolbar as an actionbar in my AppCompatActivity:

setSupportActionBar(findViewById(R.id.toolbar));

I'm able to set logo from code:

((Toolbar) findViewById(R.id.toolbar)).setLogo(R.drawable.my_logo);

But I'd like to set logo either from layout XML or from Style/Theme XML.

Lunde answered 15/6, 2015 at 17:26 Comment(4)
Your code 1. and code 2. are exactly same and app:logo="@drawable/my_logo" does set a logo in my toolbar.Hemelytron
@SunilKumar they are not the same -- the first uses android:logo, the second -- app:logoLunde
Oh, sorry. Also i concluded that we should not use toolbar if we want to place a logo in it. Toolbar does not provide any ways to customize the logo size and position.Hemelytron
@SunilKumar I believe it only allows this with a custom inner layout, as the answer below suggests.Lunde
B
13

Do Change

app:logo="@drawable/my_logo"

to

app:navigationIcon="@drawable/my_logo"

UPDATE

Add Linearlayout into your toolbar xml tag.

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/grey">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/media_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="new post image"
            android:scaleType="centerCrop"
            android:visibility="visible" />
        <TextView
            android:id="@+id/toolbar_subtitle"
            android:textSize="14sp"
            android:layout_gravity="center"
            android:textColor="@color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


</android.support.v7.widget.Toolbar>
Brennabrennan answered 15/6, 2015 at 17:58 Comment(3)
I'm not interested in customizing navigation icon ("back/up button"), but in customizing the logo (which is on the right of navigation icon).Lunde
Thanks, but I'd like to set standard logo, not introducing a custom child(ren).Lunde
I think by customization you can achieve your desired output, let me know if it would be possible with any toolbar xml attributes.Brennabrennan
R
6

Actually you can, but it will not work well with name on Toolbar. You can just put ImageView into the body of Toolbar, like you do this with Layout.

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

   <ImageView
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   app:logo="@drawable/my_logo"/>

<android.support.v7.widget.Toolbar/>

Better if you will do this from the code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.my_logo);
Racquelracquet answered 15/6, 2015 at 17:39 Comment(1)
Thanks, but I'd like to set standard logo, not introducing a custom child.Lunde
D
4

Using xml only

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

<style name="ActionbarLogo">
    <item name="android:logo">@drawable/ic_menu_camera</item>
    <item name="android:displayOptions">useLogo</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="android:actionBarStyle">@style/ActionbarLogo</item>
    <item name="windowNoTitle">true</item>
</style>

Now apply AppTheme.NoActionBar theme to your activity( and if you want to remove toolbar title use getSupportActionBar().setDisplayShowTitleEnabled(false); after setSupportActionBar(toolbar);)

<activity
  android:name=".MainActivity"
  android:theme="@style/AppTheme.NoActionBar">
  <intent-filter>
      <action android:name="android.intent.action.MAIN"/>

      <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>
</activity>
Dissident answered 3/4, 2017 at 5:3 Comment(0)
P
3

To display logo icon set this icon at runtime in ActionBar.

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(@drawable/my_logo);
Pushbike answered 15/6, 2015 at 17:31 Comment(1)
I'm interested in doing it from XML (either layout or style).Lunde
B
1

this could be the answer:

    <Toolbar
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:id="@+id/atc_toolbar"
        android:background="?android:attr/colorPrimaryDark"
        android:elevation="4dp"
        android:navigationIcon="@android:drawable/ic_menu_compass"
        android:logo="@android:drawable/ic_menu_view"
        android:title="My application">
    </Toolbar>

In my case result looks like this:

Toolbar with Logo and Navigation

If for some reasons logo does not show up then try to start a new, fresh project, add only layout_width, layout_height, id and logo attributes. Remember to add toolbar in onCreate via setActionBar or setSupportActionBar. Regards, P.

Biparous answered 7/11, 2016 at 12:39 Comment(0)
V
0

In your code put getSupportActionBar().setLogo(R.drawable.icon); It will work

Vacancy answered 30/8, 2017 at 12:8 Comment(1)
He is clearly asking for the XML way, not Java code.Hemelytron
B
0

This solved my problem. Create this file in your layout foulder:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/my_logo"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:src="@drawable/my_logo"
            android:visibility="visible" />
    </LinearLayout>

</android.support.v7.widget.Toolbar>

Do this in your activity.java:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.my_logo);

And this in your activity.xml

<include
    android:id="@+id/my_toolbar"
    layout="@layout/toolbar" />
Bipropellant answered 28/1, 2020 at 2:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.