show icon in actionbar/toolbar with AppCompat-v7 21 show icon in actionbar/toolbar with AppCompat-v7 21 android android

show icon in actionbar/toolbar with AppCompat-v7 21


getSupportActionBar().setDisplayShowHomeEnabled(true);

along with

getSupportActionBar().setIcon(R.drawable.ic_launcher);


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:

@Overridepublic 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.


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