How can I detect a click on the ActionBar title?

//onCreate

ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
//        View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
        actionBar.setCustomView(actionBarView);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

//your logic for click listner
 setListenerForActionBarCustomView(actionBarView);

 private void setListenerForActionBarCustomView(View actionBarView) {
        ActionBarCustomViewOnClickListener actionBarCustomViewOnClickListener = new ActionBarCustomViewOnClickListener();
        actionBarView.findViewById(R.id.text_view_title).setOnClickListener(actionBarCustomViewOnClickListener);
}
 private class ActionBarCustomViewOnClickListener implements OnClickListener {
        public void onClick(View v) {       
        switch(v.getId()) {
            case R.id.text_view_title:

                //finish();
                break;
    }
}

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


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

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- We use a Toolbar so that our drawer can be displayed
             in front of the action bar -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/main_toolbar" 
            android:minHeight="?attr/actionBarSize" />

       <FrameLayout 
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


</LinearLayout>

After you can add on click listener in your activity just like to most other Views.

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyActivity.this, "Test", Toast.LENGTH_LONG).show();
            }
        });

If you want to capture touch events on title:

toolbar.setOnTouchListener(new View.OnTouchListener() {
            Rect hitrect = new Rect();
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    boolean hit = false;
                    for (int i = toolbar.getChildCount() - 1; i != -1; i--) {
                        View view = toolbar.getChildAt(i);
                        if (view instanceof TextView) {
                            view.getHitRect(hitrect);
                            if (hitrect.contains((int)event.getX(), (int)event.getY())) {
                                hit = true;
                                break;
                            }
                        }
                    }
                    if (hit) {
                        //Hit action
                    }
                }
                return false;
            }
        });