How to click the views behind the toolbar?

I have a toolbar with a transparent / translucent background that overlays the content. Thus, views that can be clicked can be displayed behind the toolbar. The problem is that they cannot be clicked through the toolbar because the toolbar captures the click event.

I tried setting android:clickable="false" , android:focusable="false" and android:focusableInTouchMode="false" for the toolbar, but this does not affect. How can I send a click on a toolbar in the main view?

+7
android android-actionbar material-design android-support-library android-toolbar
source share
3 answers

Take a look at the Toolbar implementation. It eats touch events, regardless of the clickable attribute.

 @Override public boolean onTouchEvent(MotionEvent ev) { // Toolbars always eat touch events, but should still respect the touch event dispatch // contract. If the normal View implementation doesn't want the events, we'll just silently // eat the rest of the gesture without reporting the events to the default implementation // since that what it expects. final int action = MotionEventCompat.getActionMasked(ev); if (action == MotionEvent.ACTION_DOWN) { mEatingTouch = false; } if (!mEatingTouch) { final boolean handled = super.onTouchEvent(ev); if (action == MotionEvent.ACTION_DOWN && !handled) { mEatingTouch = true; } } if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { mEatingTouch = false; } return true; } 

The solution is to go from Toolbar and override onTouchEvent .

 public class NonClickableToolbar extends Toolbar { @Override public boolean onTouchEvent(MotionEvent ev) { return false; } } 
+18
source share

The toolbar consumes all clicks. You need to subclass the toolbar, as @Matthias Robbens already mentioned.

If you still want to customize the toolbar click listener, use this:

 /** Do not eat touch events, like super does. Instead map an ACTION_DOWN to a click on this * view. If no click listener is set (default), we do not consume the click */ @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_POINTER_DOWN){ return performClick(); } return false; } 
0
source share

Your setting should be adjusted. You cannot provide interactive views behind a transparent / translucent toolbar. You need to provide a top pad equal to the height of the toolbar and / or implement a quick return template for the toolbar.

-7
source share

All Articles