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"> <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) {
Boris Treukhov Nov 10 '15 at 13:33 2015-11-10 13:33
source share