Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom icon in Android toolbar

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"
    />
like image 216
Antoine Avatar asked Oct 19 '14 09:10

Antoine


People also ask

How do I add an icon to the Android toolbar?

Now if you want to add other resolutions pictures to this drawable, just do it in the same manner. Step 5) And now go to the other folder which is Settings -> Androids-> drawable hdpi. And then go to your Android, then drawable, paste it here and inside this drawable-hdpi and click Ok.

How do I change the toolbar in Android Studio?

Right-click on the open space of the toolbar (your first image) and select Customize Menus and Toolbars... . Now open the Main Toolbar folder, select any item and then click the Add After... button (on the right). Find what you want to add from the list of items available (you can also choose an icon) and click OK .


2 Answers

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.

like image 160
reVerse Avatar answered Oct 23 '22 11:10

reVerse


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>
    
like image 1
Jakub S. Avatar answered Oct 23 '22 09:10

Jakub S.