1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.car.widget; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import androidx.appcompat.widget.Toolbar; 22 import android.util.AttributeSet; 23 import android.view.MotionEvent; 24 25 import androidx.car.R; 26 27 /** 28 * A toolbar that optionally supports allowing clicks on it to pass through to any underlying views. 29 * 30 * <p>By default, the {@link Toolbar} eats all touches on it. This view will override 31 * {@link #onTouchEvent(MotionEvent)} and return {@code false} if configured to allow pass through. 32 */ 33 public class ClickThroughToolbar extends Toolbar { 34 private boolean mAllowClickPassThrough; 35 36 public ClickThroughToolbar(Context context) { 37 super(context); 38 } 39 40 public ClickThroughToolbar(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 initAttributes(context, attrs, 0 /* defStyleAttrs */); 43 } 44 45 public ClickThroughToolbar(Context context, AttributeSet attrs, int defStyleAttrs) { 46 super(context, attrs, defStyleAttrs); 47 initAttributes(context, attrs, defStyleAttrs); 48 } 49 50 private void initAttributes(Context context, AttributeSet attrs, int defStyleAttrs) { 51 TypedArray a = context.obtainStyledAttributes( 52 attrs, R.styleable.ClickThroughToolbar, defStyleAttrs, 0 /* defStyleRes */); 53 54 mAllowClickPassThrough = a.getBoolean(R.styleable.ClickThroughToolbar_clickThrough, false); 55 56 a.recycle(); 57 } 58 59 /** 60 * Whether or not clicks on this toolbar will pass through to any views that are underneath 61 * it. By default, this value is {@code false}. 62 * 63 * @param allowPassThrough {@code true} if clicks will pass through to an underlying view; 64 * {@code false} otherwise. 65 */ 66 public void setClickPassThrough(boolean allowPassThrough) { 67 mAllowClickPassThrough = allowPassThrough; 68 } 69 70 @Override 71 public boolean onTouchEvent(MotionEvent ev) { 72 if (mAllowClickPassThrough) { 73 return false; 74 } 75 76 return super.onTouchEvent(ev); 77 } 78 } 79