Difference Between setDisplayHomeAsUpEnabled(), setHomeButtonEnabled() and setDisplayShowHomeEnabled() ActionBar Methods in Android

The Android ActionBar has a couple of methods that can get a little confusing at times as to what their purpose is:

We’ll see how each differs from others.

setDisplayHomeAsUpEnabled(boolean showHomeAsUp)

This method makes the icon and title in the action bar clickable so that “up” (ancestral) navigation can be provided. This method will just make the icon and title pressable, but not actually add the functionality of navigating upwards. That has to be done by specifying the android:parentActivityName (takes the parent activity class name) on the activity in the manifest file.

It also adds a left-facing caret alongside the app icon.

More on Up (Ancestral) Navigation:

setHomeButtonEnabled(boolean enabled)

This method is similar to the previous one actually, except that the left-facing caret doesn’t show up unless the android:parentActivityName is specified.

Note: Both setDisplayHomeAsUpEnabled() and setHomeButtonEnabled() calls onOptionsItemSelected() on the Activity where you can check the item.getItemId() against android.R.id.home and perform some action.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == android.R.id.home) {
        Log.d(TAG, "action bar clicked");
    }

    return super.onOptionsItemSelected(item);
}

setDisplayShowHomeEnabled(boolean showHome)

This method just controls whether to show the Activity icon/logo or not. Generally shows just the title with caret if passed false and used in conjunction with setDisplayHomeAsUpEnabled().

The functionality of all these methods I’ve specified is based on my tests with the Holo theme. On Theme.AppCompat.Light.DarkActionBar (material design theme) there are certain differences in terms of the UI (but functionality is same). For example instead of a left caret, you’ll see an entire left arrow and only that’ll be clickable, not the entire arrow with the icon and title. Infact icon is not shown by default as it is deprecated as a concept. The documentation on Toolbar says – “The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer”. Although if you still want there’s a way to show the icon/logo like this:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher); // or setLogo()

Hope that helps!

Efficient Android Threading - I highly recommend this book if you want to learn how various components of the Android framework works. You'll learn a ton about Android internals and components like AsyncTask, Services, Broadcast Receivers, Executor Framework, Inter-Process Communication, etc. It also covers various OS internal topics like threads, processes, kernel and how all those components work to execute an Android app. Must read!

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

2 thoughts on “Difference Between setDisplayHomeAsUpEnabled(), setHomeButtonEnabled() and setDisplayShowHomeEnabled() ActionBar Methods in Android”

Leave a Reply

Your email address will not be published. Required fields are marked *


*