1

I am using ActionbarSherlock .

I set the title for the sherlock action bar in my java code by:

actionBar.setTitle(title);

I am wondering, how can I add event handler to the title of action bar?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354

2 Answers2

0

You can also set the title as an up indicator and handle that clicked as you would another menu item.

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Then in handling the options selected:

public boolean onOptionsItemSelected(final MenuItem item)
{
    switch (item.getItemId())
    {
        case android.R.id.home:
            // your title was clicked!
            return true;
    }
}

I should caution, your title should act like it has "up" behavior. By that I mean that consider your activities like a directory structure on your favorite OS. The back button goes to the place you just were, and this title should go "up" one directory regardless of how you got there.

xbakesx
  • 13,202
  • 6
  • 48
  • 76
  • Also if you would like to get rid of the icon you can call: getSupportActionBar().setDisplayShowHomeEnabled(false); supposing you only want your text up there. – xbakesx Aug 29 '12 at 20:19
0

There is no way to add an event with the built-in title.

However, you can add your own TextView using the setCustomView method on which you can add a listener for click.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • To add to Jake: See second answer here http://stackoverflow.com/questions/7981394/how-can-i-detect-a-click-on-the-actionbar-title – Alon Burg May 23 '13 at 20:59