For specific customer requirement, I need to allow the user of my app ( won't be published in Market) to click on the ActionBar title to execute some actions.
I have been looking in the Android source, but I am not able to find an ID for the actionBar TextView title.
Is there a proper way to handle such clicks?
The Toolbar is basically the advanced successor of the ActionBar. It is much more flexible and customizable in terms of appearance and functionality. Unlike ActionBar, its position is not hardcoded i.e., not at the top of an activity.
If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme.
The title is non-clickable AFAIK. The icon/logo is clickable -- you'll get that via android.R.id.home
in onOptionsItemSelected()
. Conceivably, the title also routes this way, though they don't mention it and I wouldn't rely upon it.
It sounds like you want a Spinner
for the user to choose the actions to execute. If so, use setListNavigationCallbacks()
. If you want to remove the title as now being superfluous, use setDisplayOptions(0, DISPLAY_SHOW_TITLE)
.
If you want something other than a Spinner
on the left side of the action bar, use setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM)
and setCustomView()
. Note that this approach is not recommended ("Avoid using custom navigation modes in the action bar"), as it may not work well with phones, particularly in portrait mode.
Another possibility would be to remove the title and use a logo instead of the icon, and in the logo have your "title" as part of the image. The whole logo should be clickable, picked up via onOptionsItemSelected()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With