Set OnClick Listener on Action Bar Title in Android Set OnClick Listener on Action Bar Title in Android android android

Set OnClick Listener on Action Bar Title in Android


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() {@Overridepublic void onClick(View v) {    //Do something    }});


You could use a custom layout for the title and assign a listener to it:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    ActionBar actionBar = getActionBar();    if (actionBar != null) {        // Disable the default and enable the custom        actionBar.setDisplayShowTitleEnabled(false);        actionBar.setDisplayShowCustomEnabled(true);        View customView = getLayoutInflater().inflate(R.layout.actionbar_title, null);        // Get the textview of the title        TextView customTitle = (TextView) customView.findViewById(R.id.actionbarTitle);        // Change the font family (optional)        customTitle.setTypeface(Typeface.MONOSPACE);        // Set the on click listener for the title        customTitle.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.w("MainActivity", "ActionBar's title clicked.");            }        });        // Apply the custom view        actionBar.setCustomView(customView);    }}

actionbar_title.xml:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center">    <TextView        android:id="@+id/actionbarTitle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="25sp"        android:text="@string/app_name"/></LinearLayout>


I think Simas's answer is the best one, but here's a hacky version in case you prefer that.

ViewTools.findActionBarTitle(getWindow().getDecorView()).setOnClickListener(...);

This one should be universal in that it works with:

  • stock Android ActionBar
  • Theme.AppCompat support ActionBar
  • v21-style setActionBar
    use <Toolbar android:id="@+id/action_bar"
    or pass in the inflated Toolbar as root
  • v21-style setSupportActionBar
    use <android.support.v7.widget.Toolbar android:id="@id/action_bar"
    or pass in the inflated Toolbar as root
  • custom Toolbar implementations may need a little adjustment,
    but then you could encapsulate this in that custom class.

Though I only tested with support:v22.

/** @param root usually Activity.getWindow().getDecorView() or your custom Toolbar */public static @Nullable View findActionBarTitle(@NonNull View root) {    return findActionBarItem(root, "action_bar_title", "mTitleTextView");}/** @param root usually Activity.getWindow().getDecorView() or your custom Toolbar */public static @Nullable View findActionBarSubTitle(@NonNull View root) {    return findActionBarItem(root, "action_bar_subtitle", "mSubtitleTextView");}private static @Nullable View findActionBarItem(@NonNull View root,        @NonNull String resourceName, @NonNull String toolbarFieldName) {    View result = findViewSupportOrAndroid(root, resourceName);    if (result == null) {        View actionBar = findViewSupportOrAndroid(root, "action_bar");        if (actionBar != null) {            result = reflectiveRead(actionBar, toolbarFieldName);        }    }    if (result == null && root.getClass().getName().endsWith("widget.Toolbar")) {        result = reflectiveRead(root, toolbarFieldName);    }    return result;}@SuppressWarnings("ConstantConditions")private static @Nullable View findViewSupportOrAndroid(@NonNull View root, @NonNull String resourceName) {    Context context = root.getContext();    View result = null;    if (result == null) {        int supportID = context.getResources().getIdentifier(resourceName, "id", context.getPackageName());        result = root.findViewById(supportID);    }    if (result == null) {        int androidID = context.getResources().getIdentifier(resourceName, "id", "android");        result = root.findViewById(androidID);    }    return result;}@SuppressWarnings("unchecked")public static <T> @Nullable T reflectiveRead(@NonNull Object object, @NonNull String fieldName) {    try {        Field field = object.getClass().getDeclaredField(fieldName);        field.setAccessible(true);        return (T)field.get(object);    } catch (Exception ex) {        Log.w("HACK", "Cannot read " + fieldName + " in " + object, ex);    }    return null;}