Android tricks – How to add an icon to your Toolbar?

Today I had to implement something which is not so popular throughout the Android universe. I had to implement a simple Toolbar with a logo on it’s left side. That’s not so popular these days, but from time to time there always will be someone who wants something like that. All in all, the end result had to be similar to the one down below:

toolbar_icon
Toolbar with icon

I ended up with two different solutions based on weather you want your icon to be on the left side of the title, or on the right side.

If you want to accomplish the result from the picture above, you will need not more than 3 rows of code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_icon);

These rows must be included in your onCreate() method of your Activity. First, we have to find our Toolbar view and then we need to set it as the support ActionBar for the current Activity. At the end we need to provide an icon for the current ActionBar. That’s all.

In case you want to add an icon on the right side of the title, then the only thing you have to do is to include an ImaveView into your xml file which contains the Toolbar widget in the following way:

Please, notice that before using the Toolbar widget you have to set your app theme to .NoActionBar.

Resources:

http://stackoverflow.com/questions/35913688/show-imageview-in-android-toolbar-at-top-left-corner-before-title

Feel free to share, comment & give your opinion on the topic!

And only if you really REALLY liked the article, you can buy me a cup of coffee! Otherwise, don’t do it! Donate $1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s