{"id":2074,"date":"2015-02-01T16:34:55","date_gmt":"2015-02-01T11:04:55","guid":{"rendered":"http:\/\/codetheory.in\/?p=2074"},"modified":"2015-02-01T16:35:41","modified_gmt":"2015-02-01T11:05:41","slug":"difference-between-setdisplayhomeasupenabled-sethomebuttonenabled-and-setdisplayshowhomeenabled","status":"publish","type":"post","link":"https:\/\/codetheory.in\/difference-between-setdisplayhomeasupenabled-sethomebuttonenabled-and-setdisplayshowhomeenabled\/","title":{"rendered":"Difference Between setDisplayHomeAsUpEnabled(), setHomeButtonEnabled() and setDisplayShowHomeEnabled() ActionBar Methods in Android"},"content":{"rendered":"
The Android ActionBar<\/a> has a couple of methods that can get a little confusing at times as to what their purpose is:<\/p>\n <\/p>\n We’ll see how each differs from others.<\/p>\n 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 It also adds a left-facing caret alongside the app icon.<\/p>\n More on Up (Ancestral) Navigation:<\/p>\n This method is similar to the previous one actually, except that the left-facing caret doesn’t show up unless the Note:<\/strong> Both This method just controls whether to show the Activity icon\/logo<\/a> or not. Generally shows just the title with caret if passed false and used in conjunction with The functionality of all these methods I’ve specified is based on my tests with the Holo theme. On Hope that helps!<\/p>\n","protected":false},"excerpt":{"rendered":" The Android ActionBar has a couple of methods that can get a little confusing at times as to what their purpose is:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[105,89],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/2074"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=2074"}],"version-history":[{"count":2,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/2074\/revisions"}],"predecessor-version":[{"id":2076,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/2074\/revisions\/2076"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=2074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=2074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=2074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
setDisplayHomeAsUpEnabled(boolean showHomeAsUp)<\/code><\/a>\n<\/li>\n
setHomeButtonEnabled(boolean enabled)<\/code><\/a>\n<\/li>\n
setDisplayShowHomeEnabled(boolean showHome)<\/code><\/a>\n<\/li>\n<\/ul>\n
setDisplayHomeAsUpEnabled(boolean showHomeAsUp)<\/h2>\n
android:parentActivityName<\/code><\/a> (takes the parent activity class name) on the activity in the manifest file.<\/p>\n
\n
setHomeButtonEnabled(boolean enabled)<\/h2>\n
android:parentActivityName<\/code> is specified.<\/p>\n
setDisplayHomeAsUpEnabled()<\/code> and
setHomeButtonEnabled()<\/code> calls
onOptionsItemSelected()<\/code><\/a> on the Activity where you can check the
item.getItemId()<\/code> against
android.R.id.home<\/code> and perform some action.<\/p>\n
\r\n@Override\r\npublic boolean onOptionsItemSelected(MenuItem item) {\r\n \/\/ Handle action bar item clicks here. The action bar will\r\n \/\/ automatically handle clicks on the Home\/Up button, so long\r\n \/\/ as you specify a parent activity in AndroidManifest.xml.\r\n int id = item.getItemId();\r\n\r\n if (id == android.R.id.home) {\r\n Log.d(TAG, "action bar clicked");\r\n }\r\n\r\n return super.onOptionsItemSelected(item);\r\n}\r\n<\/pre>\n
setDisplayShowHomeEnabled(boolean showHome)<\/h2>\n
setDisplayHomeAsUpEnabled()<\/code>.<\/p>\n
Theme.AppCompat.Light.DarkActionBar<\/code> (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<\/a> 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:<\/p>\n
\r\ngetSupportActionBar().setDisplayShowHomeEnabled(true);\r\ngetSupportActionBar().setIcon(R.drawable.ic_launcher); \/\/ or setLogo()\r\n<\/pre>\n