Custom icon in Android toolbar
Asked Answered
N

4

17

I'm trying to use define a custom icon in the support Toolbar but the only icon shown is a left arrow... I tried to set it in the layout and programmatically but the result is the same.

Here is my Activity

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_launcher);
    toolbar.setTitle("");
    setSupportActionBar(toolbar);
}

And my toolbar layout

<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/my_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:navigationIcon="@drawable/ic_action_bar"
    android:minHeight="@dimen/action_bar_size"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    />
Niels answered 19/10, 2014 at 9:17 Comment(0)
L
25

Just tried it myself and the issue seems to be that you have to call setNavigationIcon after setSupportActionBar(toolbar). Otherwise it'll only show the arrow as you've described.
So to fix this issue just change the code like this:

//...
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle("");

Note: Same goes for setting the title, contentDescription etc. I don't know if this a bug or if it is intended, but it's definitely kinda strange.

Livorno answered 19/10, 2014 at 10:47 Comment(3)
I have experienced the same behavior.Hutchins
Rather than toolbar.setTitle("");, I prefer to use getSupportActionBar().setDisplayShowTitleEnabled(false);. I think its a lot cleaner.Boyd
I have a question .. if you replace .. can you do transition in R.drawable.ic_launcher? I think that transition 'll breakFamiliarity
B
1

In case you want to change menu icon. (maybe somebody will need it)

  1. In your activity

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_info, menu);
        return true;
    }
    
  2. in your menu folder in res. (menu_info.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/menu_info_action"
            android:icon="@drawable/ic_info_icon"
            android:title="@string/information"
            app:showAsAction="ifRoom"/>
    </menu>
    
Bedder answered 15/3, 2017 at 12:27 Comment(0)
A
0

The current most efficient way to achieve this:

first display the left hand side icon correctly, call this function in onCreateView or onCreate:

protected void enableDisplayHomeAsHome(boolean active) {
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(active); // switch on the left hand icon
        actionBar.setHomeAsUpIndicator(R.drawable.ic_action_home); // replace with your custom icon
    }
}

Now you can intercept this button press in your activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home: {  //index returned when home button pressed
            homeButtonPressed();
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}
Anecdotage answered 4/9, 2018 at 3:52 Comment(0)
O
-1
ActionBar.setHomeAsUpIndicator(...);

This one works for me.

Olvera answered 12/11, 2015 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.