Skip to content

Commit cf006c5

Browse files
pekingmepaulfthomas
authored andcommitted
[Search] Made SearchBar and SearchView's container colors configurable in XML style.
PiperOrigin-RevId: 522468493
1 parent 303fabd commit cf006c5

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

docs/components/Search.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Element | Attribute | Related method(s)
9292
**Search text appearance** | `android:textAppearance` | `setTextAppearance`<br/>`getTextAppearance` | `@style/TextAppearance.Material3.SearchBar`
9393
**Search text** | `android:text` | `setText`<br/>`getText` | `null`
9494
**Search hint** | `android:hint` | `setHint`<br/>`getHint` | `null`
95+
**Color** | `app:backgroundTint` | -- | `?attr/colorSurface`
9596
**Flag for default margins** | `app:defaultMarginsEnabled` | -- | `true`
9697
**Flag for navigation icon** | `app:hideNavigationIcon` | -- | `false`
9798

@@ -209,6 +210,7 @@ Element | Attribute | Related meth
209210
**Search text appearance** | `android:textAppearance` | `setTextAppearance`<br/>`getTextAppearance` | `@style/TextAppearance.Material3.SearchBar`
210211
**Search text** | `android:text` | `setText`<br/>`getText` | `null`
211212
**Search hint** | `android:hint` | `setHint`<br/>`getHint` | `null`
213+
**Color** | `app:backgroundTint` | -- | `?attr/colorSurface`
212214
**Flag for navigation icon** | `app:hideNavigationIcon` | -- | `true`
213215
**Flag for `DrawerArrowDrawable`** | `app:useDrawerArrowDrawable` | -- | `false`
214216
**Flag for soft keyboard** | `app:autoShowKeyboard` | -- | `true`

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
172172

173173
ShapeAppearanceModel shapeAppearanceModel =
174174
ShapeAppearanceModel.builder(context, attrs, defStyleAttr, DEF_STYLE_RES).build();
175+
int backgroundColor = a.getColor(R.styleable.SearchBar_backgroundTint, 0);
175176
float elevation = a.getDimension(R.styleable.SearchBar_elevation, 0);
176177
defaultMarginsEnabled = a.getBoolean(R.styleable.SearchBar_defaultMarginsEnabled, true);
177178
defaultScrollFlagsEnabled = a.getBoolean(R.styleable.SearchBar_defaultScrollFlagsEnabled, true);
@@ -203,7 +204,7 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
203204

204205
ViewCompat.setElevation(this, elevation);
205206
initTextView(textAppearanceResId, text, hint);
206-
initBackground(shapeAppearanceModel, elevation, strokeWidth, strokeColor);
207+
initBackground(shapeAppearanceModel, backgroundColor, elevation, strokeWidth, strokeColor);
207208

208209
accessibilityManager =
209210
(AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
@@ -275,6 +276,7 @@ private void initTextView(@StyleRes int textAppearanceResId, String text, String
275276

276277
private void initBackground(
277278
ShapeAppearanceModel shapeAppearance,
279+
@ColorInt int backgroundColor,
278280
float elevation,
279281
float strokeWidth,
280282
@ColorInt int strokeColor) {
@@ -285,7 +287,6 @@ private void initBackground(
285287
backgroundShape.setStroke(strokeWidth, strokeColor);
286288
}
287289

288-
int backgroundColor = MaterialColors.getColor(this, R.attr.colorSurface);
289290
int rippleColor = MaterialColors.getColor(this, R.attr.colorControlHighlight);
290291
Drawable background;
291292
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import android.widget.ImageButton;
5050
import android.widget.TextView;
5151
import android.window.BackEvent;
52+
import androidx.annotation.ColorInt;
5253
import androidx.annotation.DrawableRes;
5354
import androidx.annotation.MenuRes;
5455
import androidx.annotation.NonNull;
@@ -157,6 +158,7 @@ public class SearchView extends FrameLayout implements CoordinatorLayout.Attache
157158
private boolean animatedNavigationIcon;
158159
private boolean animatedMenuItems;
159160
private boolean autoShowKeyboard;
161+
@ColorInt private final int backgroundColor;
160162
private boolean useWindowInsetsController;
161163
private boolean statusBarSpacerEnabledOverride;
162164
@NonNull private TransitionState currentTransitionState = TransitionState.HIDDEN;
@@ -179,6 +181,7 @@ public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, int de
179181
ThemeEnforcement.obtainStyledAttributes(
180182
context, attrs, R.styleable.SearchView, defStyleAttr, DEF_STYLE_RES);
181183

184+
backgroundColor = a.getColor(R.styleable.SearchView_backgroundTint, 0);
182185
int headerLayoutResId = a.getResourceId(R.styleable.SearchView_headerLayout, -1);
183186
int textAppearanceResId = a.getResourceId(R.styleable.SearchView_android_textAppearance, -1);
184187
String text = a.getString(R.styleable.SearchView_android_text);
@@ -330,9 +333,9 @@ private void setUpBackgroundViewElevationOverlay(float elevation) {
330333
if (elevationOverlayProvider == null || backgroundView == null) {
331334
return;
332335
}
333-
int backgroundColor =
334-
elevationOverlayProvider.compositeOverlayWithThemeSurfaceColorIfNeeded(elevation);
335-
backgroundView.setBackgroundColor(backgroundColor);
336+
int backgroundColorWithOverlay =
337+
elevationOverlayProvider.compositeOverlayIfNeeded(backgroundColor, elevation);
338+
backgroundView.setBackgroundColor(backgroundColorWithOverlay);
336339
}
337340

338341
private float getOverlayElevation() {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
<attr name="android:text"/>
3737
<!-- Hint for the main search TextView. -->
3838
<attr name="android:hint"/>
39+
<!-- Color of the container. -->
40+
<attr name="backgroundTint"/>
3941
<!-- Elevation of the SearchBar. -->
4042
<attr name="elevation"/>
4143
<!-- Whether the default margins should be applied to the SearchBar.
@@ -71,6 +73,8 @@
7173
<attr name="android:text"/>
7274
<!-- Hint for the main search EditText. -->
7375
<attr name="android:hint"/>
76+
<!-- Color of the container. -->
77+
<attr name="backgroundTint"/>
7478
<!-- Text for the search prefix which appears before the main search EditText. -->
7579
<attr name="searchPrefixText" format="string"/>
7680
<!-- Whether a DrawerArrowDrawable should be used for the navigation icon,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="NewApi">
1818

1919
<style name="Widget.Material3.SearchBar" parent="android:Widget">
20+
<item name="backgroundTint">@macro/m3_comp_search_bar_container_color</item>
2021
<item name="enforceMaterialTheme">true</item>
2122
<item name="android:minHeight">@dimen/m3_searchbar_height</item>
2223
<item name="android:paddingStart">@dimen/m3_searchbar_padding_start</item>
@@ -42,6 +43,7 @@
4243
</style>
4344

4445
<style name="Widget.Material3.SearchView" parent="android:Widget">
46+
<item name="backgroundTint">@macro/m3_comp_search_view_container_color</item>
4547
<item name="enforceMaterialTheme">true</item>
4648
<item name="android:elevation">@dimen/m3_searchview_elevation</item>
4749
<item name="android:textAppearance">@style/TextAppearance.Material3.SearchView</item>

0 commit comments

Comments
 (0)