How can I detect a click on an ActionBar header?

For a specific client requirement, I must allow the user of my application (will not be published in the Market) to click the ActionBar header to perform some actions.

I look in the Android source, but I can not find the id for the actionBar TextView header.

Is there a suitable way to handle these clicks?

+28
android textview android-actionbar onclick title
Nov 02 2018-11-11T00:
source share
3 answers

The name is not clickable AFAIK. The icon / logo can be clicked - you will get this via android.R.id.home in onOptionsItemSelected() . Of course, the name is also directed in this way, although they do not mention it, and I would not rely on it.

It looks like you want Spinner to choose what actions to take for the user. If so, use setListNavigationCallbacks() . If you want to remove the header as redundant, use setDisplayOptions(0, DISPLAY_SHOW_TITLE) .

If you want something other than Spinner on the left side of the action bar, use setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM) and setCustomView() . Please note that this approach is not recommended ( "Avoid using custom navigation modes in the action bar" ), as this may not work well with phones, particularly in portrait mode.

Another possibility is to delete the title and use the logo instead of the icon, and the logo will have its own β€œtitle” as part of the image. The entire logo should be accessible to the click, which can be selected using onOptionsItemSelected() .

+28
Nov 02 2018-11-11T00:
source share

// 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; } } 
+9
Nov 02 2018-11-11T00:
source share

You can customize your custom toolbar from the support library by declaring <android.support.v7.widget.Toolbar> in your layout (see Chris Banes answer for a complete example of a toolbar layout).

 <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 a listener to your account, as in 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 by 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; } }); 
+3
Nov 10 '15 at 13:33
source share



All Articles