Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect a click on the ActionBar title?

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?

like image 551
Waza_Be Avatar asked Nov 02 '11 13:11

Waza_Be


People also ask

What is the difference between ActionBar and toolbar?

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.

Which of the ways can be used to hide the ActionBar in Android?

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.


1 Answers

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().

like image 178
CommonsWare Avatar answered Oct 11 '22 12:10

CommonsWare