130

I tried these - but still do not see the icon like before:

getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);

It seems to work when I use custom toolbar - but that would force me to touch all layouts - is there a better way to do so?

ligi
  • 39,001
  • 44
  • 144
  • 244

15 Answers15

257
getSupportActionBar().setDisplayShowHomeEnabled(true);

along with

getSupportActionBar().setIcon(R.drawable.ic_launcher);
Derlin
  • 9,572
  • 2
  • 32
  • 53
nadavfima
  • 3,012
  • 1
  • 11
  • 9
32

In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

If you disagree you can try with:

To create the toolbar in XML:

<android.support.v7.widget.Toolbar  
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

In your activity:

@Override
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
}

Use the setLogo() method to set the icon. Code source.

LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
  • 1
    I just want to point out that currently the Play Store `Settings` Activity has the app icon in the action bar. This is still there after the Material update. – Austyn Mahoney Dec 15 '14 at 23:15
17

This worked for me:

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    getSupportActionBar().setLogo(R.drawable.ic_logo);
    getSupportActionBar().setDisplayShowTitleEnabled(false); //optional

as well as:

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_logo); //also displays wide logo
    getSupportActionBar().setDisplayShowTitleEnabled(false); //optional
Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
17

simplest thing to do; just add:

app:navigationIcon="@drawable/ic_action_navigation_menu">

to the <android.support.v7.widget.Toolbar tag

where @drawable/ic_action_navigation_menu is the name of icon

Pila
  • 5,460
  • 1
  • 19
  • 30
  • 1
    Works perfectly! This is the correct and simplest way to add the navigation icon in the toolbar. If you want to check for the navigation icon clicks, you can check for `android.R.id.home` ID in your `onOptionsItemSelected(...)`. – Sazid Jun 20 '16 at 17:52
12

A better way for setting multiple options:

setIcon/setLogo method will only work if you have set DisplayOptions Try this -

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);

You can also set options for displaying LOGO(just add constant ActionBar.DISPLAY_USE_LOGO). More information - displayOptions

Vintesh
  • 1,657
  • 1
  • 24
  • 31
6

Try using:

ActionBar ab = getSupportActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayUseLogoEnabled(true);
ab.setLogo(R.drawable.ic_launcher);
Chris Banes
  • 31,763
  • 16
  • 59
  • 50
6

For Actionbar:

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);

For Toolbar:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
GabrielBB
  • 2,479
  • 1
  • 35
  • 49
Peter Ivanov
  • 69
  • 1
  • 2
  • 5
    There is no difference between the 2 code for ActionBar and Toolbar, is this intentional? – King King Oct 22 '16 at 03:32
  • 1
    For ActionBar should be: getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back); – Darsshan Aug 29 '17 at 11:07
  • This is actually the way to go. Just using `setIcon` doesn't allow a clickable button, where as `setHomeAsUpIndicator` and handling that in `onOptionsItemSelected` works a treat. Thanks! – Joshua Pinter Nov 23 '19 at 16:26
5

if you want to set the home or back icon (not logo or static icon) so you can use

 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 getSupportActionBar().setHomeAsUpIndicator( getResources().getDrawable(R.drawable.home) );
smoothumut
  • 3,423
  • 1
  • 25
  • 35
  • this is the correct answer, since deploy appearance and behaviour – cutiko Apr 04 '16 at 23:21
  • only this gave me a normal appearance (size, margins, offset between title and icon), works ok even with vector launcher (`.xml`), like `R.mipmap.ic_launcher` or `R.mipmap.ic_launcher_round` – user924 Jun 29 '18 at 14:30
  • and I used this as logo/static icon cause other solutions works ugly – user924 Jun 29 '18 at 14:35
4
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);

OR make a XML layout call the tool_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="4dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:src="@drawable/ic_action_search"/>

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

Now in you main activity add this line

 <include
     android:id="@+id/tool_bar"
     layout="@layout/tool_bar" />
Adeel
  • 2,901
  • 7
  • 24
  • 34
Skitty
  • 1,709
  • 18
  • 21
3

If you dont want to set your toolbar as action bar using setSupportActionBar, you can add a logo next to your navigation icon (if you have a back button for example) like this:

toolbar.setLogo();

or in xml

<android.support.v7.widget.Toolbar 
    ....
    android:logo="@drawable/logo"
    app:logo="@drawable/logo"/>

And even if you have a title set on the Toolbar, the the title will still show.

Ex: The green check in the image below is the logo

Toolbar with navigation icon, logo and title

Community
  • 1
  • 1
Malek Hijazi
  • 4,112
  • 1
  • 26
  • 31
2

Try this:

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
...
    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_launcher);

so your icon will be used for Home / back
or

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
...
    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayShowHomeEnabled(true);
    actionbar.setIcon(R.drawable.ic_launcher);

for static icon

1
toolbar.setLogo(resize(logo, (int) Float.parseFloat(mContext.getResources().getDimension(R.dimen._120sdp) + ""), (int) Float.parseFloat(mContext.getResources().getDimension(R.dimen._35sdp) + "")));


public Drawable resize(Drawable image, int width, int height)
{
    Bitmap b = ((BitmapDrawable) image).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, width, height, false);
    return new BitmapDrawable(getResources(), bitmapResized);
}
Hardik Vasani
  • 876
  • 1
  • 8
  • 14
0

In Xamarin.Android you can use these:

SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
SupportActionBar.SetDisplayUseLogoEnabled(true);
SupportActionBar.SetIcon(Resource.Drawable.ic_launcher);
SupportActionBar.SetDisplayShowTitleEnabled(false);

using Android.Support.V7.App.AppCompatActivity is required.

Daniele D.
  • 2,624
  • 3
  • 36
  • 42
0

Try this. For me it worked

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Matte23
  • 36
  • 1
  • 3
0

In Kotlin I did the following to just show the icon:

supportActionBar?.setDisplayShowHomeEnabled(true)
supportActionBar?.setIcon(R.drawable.ic_icon_small)
Leon
  • 3,614
  • 1
  • 33
  • 46