Nisa K S Nisa K S - 28 days ago 41
Android Question

SetHomeButtonEnabled not working but setDisplayHomeAsUpEnabled is working

I want to add the application icon in the actionbar for all the activities in my application and on the icon click , I would like to navigate to the home page of my application.

I tried with the following code in onCreate

ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);


Now the application icon is coming in the actionbar , but on clicking it, onOptionsItemSelected is not getting called. But if use actionBar.setDisplayHomeAsUpEnabled(true) instead of actionBar.setHomeButtonEnabled(true) , onOptionsItemSelected is getting called with item.getItemId() . Below is the code snippet

ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);


The documentation says using both setDisplayHomeAsUpEnabled and setHomeButtonEnabled , onOptionsItemSelected will be called and the only difference is the up arrow. I dont need the up arrow in the actionbar, I only require the application icon. How can that be done?

My minSdkVersion 14 and targetSdkVersion 21.

Answer

From http://developer.android.com/reference/android/app/ActionBar.html#setHomeAsUpIndicator(int)

You can use:

actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher);
actionBar.setDisplayShowHomeAsUpEnabled(true);

and this should replace the back arrow with your icon