How can I use an icon instead of a title in the Android Toolbar?
Asked Answered
M

3

25

I'm using the Toolbar (instead of ActionBar) via AppCompat. I'd like to replace the Toolbar's title (the app/actity name) with an icon, but I don't see how.

My icon is just text using a non-standard font, so I guess it might be allowed in Material Design.

Morentz answered 6/11, 2014 at 16:13 Comment(0)
M
56

Use the following steps in your activity:

  1. Declare the Toolbar object:

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool1);
    
  2. Set the support actionbar:

    setSupportActionBar(toolbar);   
    
  3. Remove title:

    getSupportActionBar().setDisplayShowTitleEnabled(false);
    
  4. Add your logo in drawable folder in:

    res/drawable.
    
  5. Add logo using this code:

    toolbar.setLogo(R.drawable.ic_launcher);
    
Montemayor answered 11/11, 2014 at 12:18 Comment(3)
how to set logo dimensions/margin?Chowchow
@Chowchow how about "?attr/actionBarSize" ?Altorelievo
But what if I want to add another logo? For example, actual logo on the left and an image(image button actually) on the center.Centralization
C
0

At styles.xml first make a style for action bar with no title and with the logo like that

<style name="ActionBar.NoTitle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="displayOptions">useLogo|showHome</item>
        <item name="logo">@drawable/ic_logo</item>
    </style>

Then create custome theme for your activity with new actionbar style

<!-- Create a style for MainActivity called AppTheme.Main that uses our custom ActionBar style -->

<style name="AppTheme.Main" parent="@style/AppTheme">
    <item name="actionBarStyle">@style/ActionBar.NoTitle</item>
</style>

Then go to manifest file under the specified activity set theme to AppTheme.Main

android:theme="@style/AppTheme.Main"

I wish that help everyone

Cosme answered 26/4, 2022 at 2:0 Comment(0)
M
-1

Here is the answer to a very similar question: Android Lollipop, add popup menu from title in toolbar

It comes down to using the Toolbar as a ViewGroup (LinearLayout is a ViewGroup too, for instance). The layout designer doesn't currently support that, but you can add a child node in the XML.

Then you need to call this to remove the standard title text: getSupportActionBar().setDisplayShowTitleEnabled(false);

Morentz answered 6/11, 2014 at 20:33 Comment(1)
Actually, kunal.c's answer is much better.Morentz

© 2022 - 2024 — McMap. All rights reserved.