show icon in actionbar/toolbar with AppCompat-v7 21
AndroidAndroid AppcompatAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
getSupportActionBar().setDisplayShowHomeEnabled(true);
along with
getSupportActionBar().setIcon(R.drawable.ic_launcher);
Solution 2 - Android
> 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.
Solution 3 - Android
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
Solution 4 - Android
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
Solution 5 - Android
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
Solution 6 - Android
Try using:
ActionBar ab = getSupportActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayUseLogoEnabled(true);
ab.setLogo(R.drawable.ic_launcher);
Solution 7 - Android
For Actionbar:
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
For Toolbar:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
Solution 8 - Android
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) );
Solution 9 - Android
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" />
Solution 10 - Android
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
Solution 11 - Android
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
Solution 12 - Android
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);
}
Solution 13 - Android
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.
Solution 14 - Android
Try this. For me it worked
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
Solution 15 - Android
In Kotlin I did the following to just show the icon:
supportActionBar?.setDisplayShowHomeEnabled(true)
supportActionBar?.setIcon(R.drawable.ic_icon_small)