Skip to content

Commit

Permalink
[Search] Made SearchBar and SearchView's container colors configurabl…
Browse files Browse the repository at this point in the history
…e in XML style.

PiperOrigin-RevId: 522468493
  • Loading branch information
pekingme authored and paulfthomas committed Apr 10, 2023
1 parent 303fabd commit cf006c5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/components/Search.md
Expand Up @@ -92,6 +92,7 @@ Element | Attribute | Related method(s)
**Search text appearance** | `android:textAppearance` | `setTextAppearance`<br/>`getTextAppearance` | `@style/TextAppearance.Material3.SearchBar`
**Search text** | `android:text` | `setText`<br/>`getText` | `null`
**Search hint** | `android:hint` | `setHint`<br/>`getHint` | `null`
**Color** | `app:backgroundTint` | -- | `?attr/colorSurface`
**Flag for default margins** | `app:defaultMarginsEnabled` | -- | `true`
**Flag for navigation icon** | `app:hideNavigationIcon` | -- | `false`

Expand Down Expand Up @@ -209,6 +210,7 @@ Element | Attribute | Related meth
**Search text appearance** | `android:textAppearance` | `setTextAppearance`<br/>`getTextAppearance` | `@style/TextAppearance.Material3.SearchBar`
**Search text** | `android:text` | `setText`<br/>`getText` | `null`
**Search hint** | `android:hint` | `setHint`<br/>`getHint` | `null`
**Color** | `app:backgroundTint` | -- | `?attr/colorSurface`
**Flag for navigation icon** | `app:hideNavigationIcon` | -- | `true`
**Flag for `DrawerArrowDrawable`** | `app:useDrawerArrowDrawable` | -- | `false`
**Flag for soft keyboard** | `app:autoShowKeyboard` | -- | `true`
Expand Down
5 changes: 3 additions & 2 deletions lib/java/com/google/android/material/search/SearchBar.java
Expand Up @@ -172,6 +172,7 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def

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

ViewCompat.setElevation(this, elevation);
initTextView(textAppearanceResId, text, hint);
initBackground(shapeAppearanceModel, elevation, strokeWidth, strokeColor);
initBackground(shapeAppearanceModel, backgroundColor, elevation, strokeWidth, strokeColor);

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

private void initBackground(
ShapeAppearanceModel shapeAppearance,
@ColorInt int backgroundColor,
float elevation,
float strokeWidth,
@ColorInt int strokeColor) {
Expand All @@ -285,7 +287,6 @@ private void initBackground(
backgroundShape.setStroke(strokeWidth, strokeColor);
}

int backgroundColor = MaterialColors.getColor(this, R.attr.colorSurface);
int rippleColor = MaterialColors.getColor(this, R.attr.colorControlHighlight);
Drawable background;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand Down
9 changes: 6 additions & 3 deletions lib/java/com/google/android/material/search/SearchView.java
Expand Up @@ -49,6 +49,7 @@
import android.widget.ImageButton;
import android.widget.TextView;
import android.window.BackEvent;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.MenuRes;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -157,6 +158,7 @@ public class SearchView extends FrameLayout implements CoordinatorLayout.Attache
private boolean animatedNavigationIcon;
private boolean animatedMenuItems;
private boolean autoShowKeyboard;
@ColorInt private final int backgroundColor;
private boolean useWindowInsetsController;
private boolean statusBarSpacerEnabledOverride;
@NonNull private TransitionState currentTransitionState = TransitionState.HIDDEN;
Expand All @@ -179,6 +181,7 @@ public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, int de
ThemeEnforcement.obtainStyledAttributes(
context, attrs, R.styleable.SearchView, defStyleAttr, DEF_STYLE_RES);

backgroundColor = a.getColor(R.styleable.SearchView_backgroundTint, 0);
int headerLayoutResId = a.getResourceId(R.styleable.SearchView_headerLayout, -1);
int textAppearanceResId = a.getResourceId(R.styleable.SearchView_android_textAppearance, -1);
String text = a.getString(R.styleable.SearchView_android_text);
Expand Down Expand Up @@ -330,9 +333,9 @@ private void setUpBackgroundViewElevationOverlay(float elevation) {
if (elevationOverlayProvider == null || backgroundView == null) {
return;
}
int backgroundColor =
elevationOverlayProvider.compositeOverlayWithThemeSurfaceColorIfNeeded(elevation);
backgroundView.setBackgroundColor(backgroundColor);
int backgroundColorWithOverlay =
elevationOverlayProvider.compositeOverlayIfNeeded(backgroundColor, elevation);
backgroundView.setBackgroundColor(backgroundColorWithOverlay);
}

private float getOverlayElevation() {
Expand Down
Expand Up @@ -36,6 +36,8 @@
<attr name="android:text"/>
<!-- Hint for the main search TextView. -->
<attr name="android:hint"/>
<!-- Color of the container. -->
<attr name="backgroundTint"/>
<!-- Elevation of the SearchBar. -->
<attr name="elevation"/>
<!-- Whether the default margins should be applied to the SearchBar.
Expand Down Expand Up @@ -71,6 +73,8 @@
<attr name="android:text"/>
<!-- Hint for the main search EditText. -->
<attr name="android:hint"/>
<!-- Color of the container. -->
<attr name="backgroundTint"/>
<!-- Text for the search prefix which appears before the main search EditText. -->
<attr name="searchPrefixText" format="string"/>
<!-- Whether a DrawerArrowDrawable should be used for the navigation icon,
Expand Down
Expand Up @@ -17,6 +17,7 @@
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="NewApi">

<style name="Widget.Material3.SearchBar" parent="android:Widget">
<item name="backgroundTint">@macro/m3_comp_search_bar_container_color</item>
<item name="enforceMaterialTheme">true</item>
<item name="android:minHeight">@dimen/m3_searchbar_height</item>
<item name="android:paddingStart">@dimen/m3_searchbar_padding_start</item>
Expand All @@ -42,6 +43,7 @@
</style>

<style name="Widget.Material3.SearchView" parent="android:Widget">
<item name="backgroundTint">@macro/m3_comp_search_view_container_color</item>
<item name="enforceMaterialTheme">true</item>
<item name="android:elevation">@dimen/m3_searchview_elevation</item>
<item name="android:textAppearance">@style/TextAppearance.Material3.SearchView</item>
Expand Down

0 comments on commit cf006c5

Please sign in to comment.