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?
You can set up a custom toolbar from Support Library by declaring
<android.support.v7.widget.Toolbar>
in your layout (see Chris Banes' answer for full toolbar layout example).After you can add on click listener in your activity just like to most other Views.
If you want to capture touch events on title:
//onCreate
The title is non-clickable AFAIK. The icon/logo is clickable -- you'll get that via
android.R.id.home
inonOptionsItemSelected()
. 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, usesetListNavigationCallbacks()
. If you want to remove the title as now being superfluous, usesetDisplayOptions(0, DISPLAY_SHOW_TITLE)
.If you want something other than a
Spinner
on the left side of the action bar, usesetDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM)
andsetCustomView()
. 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()
.