Williams Williams - 2 months ago 86
Android Question

Set OnClick Listener on Action Bar Title in Android

I am working on android application where I am using

ActionBar
so there one is navigation drawer icon to open it and title of
ActionBar
in
ActionBar
. I want to set a click listener on title of
ActionBar
such that it start a new
Activity
and set click listener different on navigation drawer icon to open navigation drawer menu.

I achieved a click on navigation drawer icon but when I click on title of
ActionBar
title also then it open the navigation drawer menu. Is there any way to set different click listener on title of
ActionBar
.

Thanks in advance.

Answer

Try adding this code under the onCreate() function. This will grab the resource the action bar title is under, and assign it an id you can use to add an OnClickListener to. Let me know how it goes!

final int abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
findViewById(abTitleId).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    //Do something
    }
});
Comments