Skip to content

Commit 43f2b90

Browse files
imhappipekingme
authored andcommitted
[SearchBar] Add attribute to control when to impose max width based on a percentage of available width in parent
PiperOrigin-RevId: 758280412
1 parent ca46cd8 commit 43f2b90

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

docs/components/Search.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,20 @@ The following is an anatomy diagram for the search bar:
9999

100100
The following attributes can be changed for `SearchBar`:
101101

102-
Element | Attribute | Related method(s) | Default value
103-
---------------------------- | --------------------------- | ------------------------------------------- | -------------
104-
**Max Width** | `android:maxWidth` | `setMaxWidth`<br/>`getMaxWidth` | `-1` (unset)
105-
**Min height** | `android:minHeight` | `setMinHeight`<br/>`getMinHeight` | `@dimen/m3_searchbar_height`
106-
**Search text appearance** | `android:textAppearance` | `setTextAppearance`<br/>`getTextAppearance` | `@style/TextAppearance.Material3.SearchBar`
107-
**Search text** | `android:text` | `setText`<br/>`getText` | `null`
108-
**Search hint** | `android:hint` | `setHint`<br/>`getHint` | `null`
109-
**Search text centered** | `app:textCentered` | `setTextCentered`<br/>`getTextCentered` | `false`
110-
**Color** | `app:backgroundTint` | -- | `?attr/colorSurfaceContainerHigh`
111-
**Lift On Scroll** | `app:liftOnScroll` | -- | `false`
112-
**Lift On Scroll Color** | `app:liftOnScrollColor` | -- | `?attr/colorSurfaceContainerHighest`
113-
**Flag for default margins** | `app:defaultMarginsEnabled` | -- | `true`
114-
**Flag for navigation icon** | `app:hideNavigationIcon` | -- | `false`
102+
Element | Attribute | Related method(s) | Default value
103+
---------------------------------------- | ----------------------------- | ------------------------------------------- | -------------
104+
**Max Width** | `android:maxWidth` | `setMaxWidth`<br/>`getMaxWidth` | `-1` (unset)
105+
**Flag for enabling adaptive max width** | `app:adaptiveMaxWidthEnabled` | -- | `false`
106+
**Min height** | `android:minHeight` | `setMinHeight`<br/>`getMinHeight` | `@dimen/m3_searchbar_height`
107+
**Search text appearance** | `android:textAppearance` | `setTextAppearance`<br/>`getTextAppearance` | `@style/TextAppearance.Material3.SearchBar`
108+
**Search text** | `android:text` | `setText`<br/>`getText` | `null`
109+
**Search hint** | `android:hint` | `setHint`<br/>`getHint` | `null`
110+
**Search text centered** | `app:textCentered` | `setTextCentered`<br/>`getTextCentered` | `false`
111+
**Color** | `app:backgroundTint` | -- | `?attr/colorSurfaceContainerHigh`
112+
**Lift On Scroll** | `app:liftOnScroll` | -- | `false`
113+
**Lift On Scroll Color** | `app:liftOnScrollColor` | -- | `?attr/colorSurfaceContainerHighest`
114+
**Flag for default margins** | `app:defaultMarginsEnabled` | -- | `true`
115+
**Flag for navigation icon** | `app:hideNavigationIcon` | -- | `false`
115116

116117
## Styles
117118

lib/java/com/google/android/material/search/SearchBar.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,15 @@ public class SearchBar extends Toolbar {
157157
private MaterialShapeDrawable backgroundShape;
158158
private boolean textCentered;
159159
private int maxWidth;
160+
private final boolean adaptiveMaxWidthEnabled;
161+
private final int adaptiveMaxWidthParentBreakpoint;
160162
@Nullable private ActionMenuView menuView;
161163
@Nullable private ImageButton navIconButton;
162164

165+
// The percentage of the available width that the SearchBar should be at after the specified
166+
// breakpoint in the measure pass.
167+
private static final float ADAPTIVE_MAX_WIDTH_PERCENT_AFTER_BREAKPOINT = 0.5f;
168+
163169
private final LiftOnScrollProgressListener liftColorListener =
164170
new LiftOnScrollProgressListener() {
165171

@@ -187,6 +193,8 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
187193
context = getContext();
188194
validateAttributes(attrs);
189195

196+
adaptiveMaxWidthParentBreakpoint =
197+
getResources().getDimensionPixelSize(R.dimen.m3_searchbar_parent_width_breakpoint);
190198
defaultNavigationIcon =
191199
AppCompatResources.getDrawable(context, getDefaultNavigationIconResource());
192200
searchBarAnimationHelper = new SearchBarAnimationHelper();
@@ -218,6 +226,7 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
218226
textCentered = a.getBoolean(R.styleable.SearchBar_textCentered, false);
219227
liftOnScroll = a.getBoolean(R.styleable.SearchBar_liftOnScroll, false);
220228
maxWidth = a.getDimensionPixelSize(R.styleable.SearchBar_android_maxWidth, -1);
229+
adaptiveMaxWidthEnabled = a.getBoolean(R.styleable.SearchBar_adaptiveMaxWidthEnabled, false);
221230

222231
a.recycle();
223232

@@ -419,9 +428,16 @@ public void inflateMenu(@MenuRes int resId) {
419428

420429
@Override
421430
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
422-
if (maxWidth >= 0 && maxWidth < MeasureSpec.getSize(widthMeasureSpec)) {
423-
int measureMode = MeasureSpec.getMode(widthMeasureSpec);
431+
int availableWidth = MeasureSpec.getSize(widthMeasureSpec);
432+
int measureMode = MeasureSpec.getMode(widthMeasureSpec);
433+
if (maxWidth >= 0 && availableWidth > maxWidth) {
424434
widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, measureMode);
435+
} else if (adaptiveMaxWidthEnabled && availableWidth > adaptiveMaxWidthParentBreakpoint) {
436+
int newWidth =
437+
max(
438+
adaptiveMaxWidthParentBreakpoint,
439+
Math.round(ADAPTIVE_MAX_WIDTH_PERCENT_AFTER_BREAKPOINT * availableWidth));
440+
widthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, measureMode);
425441
}
426442
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
427443

lib/java/com/google/android/material/search/res-public/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<public name="materialSearchViewToolbarHeight" type="attr"/>
2525
<public name="defaultMarginsEnabled" type="attr"/>
2626
<public name="defaultScrollFlagsEnabled" type="attr"/>
27+
<public name="adaptiveMaxWidthEnabled" type="attr"/>
2728
<public name="hideNavigationIcon" type="attr"/>
2829
<public name="forceDefaultNavigationOnClickListener" type="attr"/>
2930
<public name="tintNavigationIcon" type="attr"/>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@
7171
<attr name="liftOnScroll"/>
7272
<!-- The color of the SearchBar when lifted. If null, the search bar color will not change. -->
7373
<attr name="liftOnScrollColor"/>
74-
<!-- The max width the SearchBar can be. If the SearchBar width is set explicitly, it will ignore this value. -->
74+
<!-- The max width the SearchBar can be. -->
7575
<attr name="android:maxWidth"/>
76+
<!-- Whether or not to set an adaptive maximum width on the SearchBar based on the available width in the parent view.
77+
If the max width is set explicitly, it will take precedence over this attribute. -->
78+
<attr name="adaptiveMaxWidthEnabled" format="boolean"/>
7679
</declare-styleable>
7780

7881
<declare-styleable name="SearchView">

lib/java/com/google/android/material/search/res/values/dimens.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@
2828
<dimen name="m3_searchview_elevation">@dimen/m3_comp_search_view_container_elevation</dimen>
2929
<dimen name="m3_searchview_divider_size">1dp</dimen>
3030

31+
<dimen name="m3_searchbar_parent_width_breakpoint">312dp</dimen>
32+
3133
</resources>

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<item name="liftOnScroll">true</item>
2525
<item name="contentInsetStart">24dp</item>
2626
<item name="android:paddingEnd">4dp</item>
27-
<item name="android:maxWidth">720dp</item>
2827
</style>
2928

3029
<style name="Widget.Material3Expressive.SearchBar.CenteredText">
@@ -43,20 +42,20 @@
4342
<style name="Widget.Material3Expressive.SearchView" parent="Widget.Material3.SearchView"/>
4443

4544
<style name="Widget.Material3Expressive.SearchBar.CenteredText.AppBarWithSearch">
46-
<item name="android:maxWidth">312dp</item>
4745
<item name="defaultMarginsEnabled">false</item>
4846
<item name="android:layout_gravity">center</item>
4947
<item name="android:layout_marginStart">8dp</item>
5048
<item name="android:layout_marginEnd">8dp</item>
49+
<item name="adaptiveMaxWidthEnabled">true</item>
5150
</style>
5251

5352
<style name="Widget.Material3Expressive.SearchBar.AppBarWithSearch">
54-
<item name="android:maxWidth">312dp</item>
5553
<item name="defaultMarginsEnabled">false</item>
5654
<item name="hideNavigationIcon">true</item>
5755
<item name="android:layout_gravity">center</item>
5856
<item name="android:layout_marginStart">8dp</item>
5957
<item name="android:layout_marginEnd">8dp</item>
58+
<item name="adaptiveMaxWidthEnabled">true</item>
6059
</style>
6160

6261
<style name="Widget.Material3Expressive.AppBarLayout.AppBarWithSearch">

0 commit comments

Comments
 (0)