Skip to content

Commit 05bc557

Browse files
ymariancketcham
authored andcommitted
Add a way to disable min touch target size in fab
PiperOrigin-RevId: 239395098
1 parent b3793ed commit 05bc557

File tree

10 files changed

+175
-11
lines changed

10 files changed

+175
-11
lines changed

lib/java/com/google/android/material/chip/Chip.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,6 @@ private void updateAccessibilityDelegate() {
277277
}
278278
}
279279

280-
private boolean shouldEnsureMinTouchTargetSize() {
281-
return ensureMinTouchTargetSize;
282-
}
283-
284280
private void initMinTouchTarget(Context context, AttributeSet attrs, int defStyleAttr) {
285281
if (attrs == null) {
286282
return;
@@ -2257,7 +2253,7 @@ public void setChipEndPadding(float chipEndPadding) {
22572253
* @see #setEnsureMinTouchTargetSize(boolean)
22582254
* @attr ref com.google.android.material.R.styleable#Chip_ensureMinTouchTargetSize
22592255
*/
2260-
public boolean getEnsureMinTouchTargetSize() {
2256+
public boolean shouldEnsureMinTouchTargetSize() {
22612257
return ensureMinTouchTargetSize;
22622258
}
22632259

lib/java/com/google/android/material/chip/res/values/attrs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<!-- Minimum size of chip's touch target, by default, Android recommended 48dp. -->
4242
<attr name="chipMinTouchTargetSize" format="dimension"/>
4343
<!-- Whether to extend the bounds of chip to meet chipMinTouchTargetSize. -->
44-
<attr name="ensureMinTouchTargetSize" format="boolean"/>
44+
<attr name="ensureMinTouchTargetSize"/>
4545

4646
<!-- Text to display on the chip. -->
4747
<attr name="android:text"/>

lib/java/com/google/android/material/floatingactionbutton/FloatingActionButton.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ public FloatingActionButton(Context context, @Nullable AttributeSet attrs, int d
236236
new ShapeAppearanceModel(context, attrs, defStyleAttr, DEF_STYLE_RES, -1);
237237

238238
boolean usingDefaultCorner = isUsingDefaultCorner(shapeAppearance);
239+
boolean ensureMinTouchTargetSize = a
240+
.getBoolean(R.styleable.FloatingActionButton_ensureMinTouchTargetSize, false);
241+
239242
a.recycle();
240243

241244
imageHelper = new AppCompatImageHelper(this);
@@ -252,6 +255,7 @@ public FloatingActionButton(Context context, @Nullable AttributeSet attrs, int d
252255
getImpl().setMaxImageSize(maxImageSize);
253256
getImpl().setShowMotionSpec(showMotionSpec);
254257
getImpl().setHideMotionSpec(hideMotionSpec);
258+
getImpl().setEnsureMinTouchTargetSize(ensureMinTouchTargetSize);
255259

256260
setScaleType(ScaleType.MATRIX);
257261
}
@@ -514,6 +518,31 @@ public ShapeAppearanceModel getShapeAppearance() {
514518
return checkNotNull(getImpl().getShapeAppearance());
515519
}
516520

521+
/**
522+
* Returns whether this fab will expand its bounds (if needed) to meet the minimum touch target
523+
* size.
524+
*
525+
* @see #setEnsureMinTouchTargetSize(boolean)
526+
* @attr ref com.google.android.material.R.styleable#FloatingActionButton_ensureMinTouchTargetSize
527+
*/
528+
public boolean shouldEnsureMinTouchTargetSize() {
529+
return getImpl().getEnsureMinTouchTargetSize();
530+
}
531+
532+
/**
533+
* Sets whether this FloatingActionButton should expand its bounds (if needed) to meet the minimum
534+
* touch target size.
535+
*
536+
* @attr ref com.google.android.material.R.styleable#FloatingActionButton_ensureMinTouchTargetSize
537+
*/
538+
public void setEnsureMinTouchTargetSize(boolean flag) {
539+
if (flag != getImpl().getEnsureMinTouchTargetSize()) {
540+
getImpl().setEnsureMinTouchTargetSize(flag);
541+
requestLayout();
542+
}
543+
}
544+
545+
517546
@Override
518547
public void setVisibility(int visibility) {
519548
super.setVisibility(visibility);

lib/java/com/google/android/material/floatingactionbutton/FloatingActionButtonImpl.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class FloatingActionButtonImpl {
8383
@Nullable Drawable contentBackground;
8484

8585
boolean usingDefaultCorner;
86+
boolean ensureMinTouchTargetSize;
8687
float elevation;
8788
float hoveredFocusedTranslationZ;
8889
float pressedTranslationZ;
@@ -341,8 +342,16 @@ final void setHideMotionSpec(@Nullable MotionSpec spec) {
341342
hideMotionSpec = spec;
342343
}
343344

344-
final boolean isAccessible() {
345-
return view.getSizeDimension() >= minTouchTargetSize;
345+
final boolean shouldExpandBoundsForA11y() {
346+
return !ensureMinTouchTargetSize || view.getSizeDimension() >= minTouchTargetSize;
347+
}
348+
349+
boolean getEnsureMinTouchTargetSize() {
350+
return ensureMinTouchTargetSize;
351+
}
352+
353+
void setEnsureMinTouchTargetSize(boolean flag) {
354+
ensureMinTouchTargetSize = flag;
346355
}
347356

348357
void onElevationsChanged(
@@ -641,7 +650,10 @@ final void updatePadding() {
641650
}
642651

643652
void getPadding(Rect rect) {
644-
final int minPadding = (minTouchTargetSize - view.getSizeDimension()) / 2;
653+
final int minPadding = ensureMinTouchTargetSize
654+
? (minTouchTargetSize - view.getSizeDimension()) / 2
655+
: 0;
656+
645657
final float maxShadowSize = (getElevation() + pressedTranslationZ);
646658
final int hPadding = Math.max(minPadding, (int) Math.ceil(maxShadowSize));
647659
final int vPadding = Math.max(minPadding, (int) Math.ceil(maxShadowSize * SHADOW_MULTIPLIER));

lib/java/com/google/android/material/floatingactionbutton/FloatingActionButtonImplLollipop.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void onCompatShadowChanged() {
173173

174174
@Override
175175
boolean shouldAddPadding() {
176-
return shadowViewDelegate.isCompatPaddingEnabled() || !isAccessible();
176+
return shadowViewDelegate.isCompatPaddingEnabled() || !shouldExpandBoundsForA11y();
177177
}
178178

179179
@Override
@@ -236,7 +236,7 @@ MaterialShapeDrawable createShapeDrawable() {
236236
void getPadding(Rect rect) {
237237
if (shadowViewDelegate.isCompatPaddingEnabled()) {
238238
super.getPadding(rect);
239-
} else if (!isAccessible()) {
239+
} else if (!shouldExpandBoundsForA11y()) {
240240
int minPadding = (minTouchTargetSize - view.getSizeDimension()) / 2;
241241
rect.set(minPadding, minPadding, minPadding, minPadding);
242242
} else {

lib/java/com/google/android/material/floatingactionbutton/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<attr name="fabCustomSize" format="dimension"/>
3737
<!-- Elevation value for the FAB -->
3838
<attr name="elevation"/>
39+
<!-- Whether to extend the bounds of the FloatingActionButton to meet
40+
@dimen/mtrl_fab_min_touch_target. -->
41+
<attr name="ensureMinTouchTargetSize"/>
3942
<!-- TranslationZ value for the FAB when hovered, focused, or hovered and focused. -->
4043
<attr name="hoveredFocusedTranslationZ" format="dimension"/>
4144
<!-- TranslationZ value for the FAB when pressed-->

lib/java/com/google/android/material/floatingactionbutton/res/values/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<style name="Widget.MaterialComponents.FloatingActionButton" parent="Widget.Design.FloatingActionButton">
3636
<item name="android:background">@null</item>
3737
<item name="enforceMaterialTheme">true</item>
38+
<item name="ensureMinTouchTargetSize">true</item>
3839
<item name="elevation">@dimen/mtrl_fab_elevation</item>
3940
<item name="backgroundTint">?attr/colorSecondary</item>
4041
<item name="tint">?attr/colorOnSecondary</item>

lib/java/com/google/android/material/resources/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
<attr name="singleSelection" format="boolean"/>
2222
<attr name="strokeColor" format="color"/>
2323
<attr name="strokeWidth" format="dimension"/>
24+
<attr name="ensureMinTouchTargetSize" format="boolean"/>
2425
</resources>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2019 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+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools"
19+
package="com.google.android.material.floatingactionbutton">
20+
21+
<uses-sdk
22+
tools:overrideLibrary="androidx.test.core"/>
23+
24+
<application/>
25+
</manifest>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) 2019 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 com.google.android.material.floatingactionbutton;
18+
19+
import com.google.android.material.R;
20+
21+
import static android.os.Build.VERSION_CODES.LOLLIPOP;
22+
import static com.google.android.material.floatingactionbutton.FloatingActionButton.SIZE_MINI;
23+
import static com.google.android.material.internal.ViewUtils.dpToPx;
24+
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertNotEquals;
26+
import static org.junit.Assert.assertTrue;
27+
28+
import android.content.Context;
29+
import androidx.appcompat.app.AppCompatActivity;
30+
import android.view.View.MeasureSpec;
31+
import androidx.test.core.app.ApplicationProvider;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.robolectric.Robolectric;
36+
import org.robolectric.RobolectricTestRunner;
37+
import org.robolectric.annotation.Config;
38+
import org.robolectric.annotation.internal.DoNotInstrument;
39+
40+
@RunWith(RobolectricTestRunner.class)
41+
@Config(sdk = LOLLIPOP)
42+
@DoNotInstrument
43+
public class FabTest {
44+
45+
private static final double DELTA = 0.01;
46+
private static final int MIN_SIZE_FOR_ALLY_DP = 48;
47+
private Context activity;
48+
49+
@Before
50+
public void createAndThemeApplicationContext() {
51+
ApplicationProvider.getApplicationContext().setTheme(
52+
R.style.Theme_MaterialComponents_Light_NoActionBar_Bridge);
53+
activity = Robolectric.buildActivity(AppCompatActivity.class).setup().get();
54+
}
55+
56+
@Test
57+
public void ensureMinTouchTarget_is48dp() {
58+
FloatingActionButton fab = createFabForTest(true);
59+
60+
float expectedSize = dpToPx(activity, MIN_SIZE_FOR_ALLY_DP);
61+
62+
android.util.Log.i("marian", String.valueOf(expectedSize));
63+
64+
assertEquals(
65+
"Fab width was: " + fab.getMeasuredWidth(),
66+
expectedSize, fab.getMeasuredWidth(), DELTA);
67+
68+
assertEquals(
69+
"Fab width was: " + fab.getMeasuredHeight(),
70+
expectedSize, fab.getMeasuredHeight(), DELTA);
71+
}
72+
73+
@Test
74+
public void ensureMinTouchTargetFalse_isLessThan48dp() {
75+
FloatingActionButton fab = createFabForTest(false);
76+
77+
float minSize = dpToPx(activity, MIN_SIZE_FOR_ALLY_DP);
78+
79+
assertNotEquals(fab.getMeasuredWidth(), minSize, DELTA);
80+
81+
assertTrue(
82+
"Fab width was: " + fab.getMeasuredWidth(),
83+
fab.getMeasuredWidth() < minSize);
84+
85+
assertTrue(fab.getMeasuredHeight() < minSize);
86+
}
87+
88+
private FloatingActionButton createFabForTest(boolean ensureMinTouchTarget) {
89+
FloatingActionButton fab = new FloatingActionButton(activity);
90+
float dimen = dpToPx(activity, MIN_SIZE_FOR_ALLY_DP);
91+
fab.setSize(SIZE_MINI);
92+
fab.setEnsureMinTouchTargetSize(ensureMinTouchTarget);
93+
int measureSpec = MeasureSpec.makeMeasureSpec((int) (dimen * 2), MeasureSpec.AT_MOST);
94+
fab.measure(measureSpec, measureSpec);
95+
return fab;
96+
}
97+
}

0 commit comments

Comments
 (0)