diff --git a/lib/java/com/google/android/material/search/SearchBar.java b/lib/java/com/google/android/material/search/SearchBar.java index 3c104fdcd3e..829bcb97fed 100644 --- a/lib/java/com/google/android/material/search/SearchBar.java +++ b/lib/java/com/google/android/material/search/SearchBar.java @@ -18,6 +18,7 @@ import com.google.android.material.R; +import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap; import android.animation.AnimatorListenerAdapter; @@ -47,11 +48,14 @@ import android.widget.ImageButton; import android.widget.TextView; import androidx.annotation.ColorInt; +import androidx.annotation.DimenRes; import androidx.annotation.Dimension; +import androidx.annotation.DrawableRes; import androidx.annotation.MenuRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import androidx.annotation.RestrictTo; import androidx.annotation.StringRes; import androidx.annotation.StyleRes; import androidx.coordinatorlayout.widget.CoordinatorLayout; @@ -159,7 +163,7 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def validateAttributes(attrs); defaultNavigationIcon = - AppCompatResources.getDrawable(context, R.drawable.ic_search_black_24); + AppCompatResources.getDrawable(context, getDefaultNavigationIconResource()); searchBarAnimationHelper = new SearchBarAnimationHelper(); TypedArray a = @@ -195,7 +199,7 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def LayoutInflater.from(context).inflate(R.layout.mtrl_search_bar, this); layoutInflated = true; - textView = findViewById(R.id.search_bar_text_view); + textView = findViewById(R.id.open_search_bar_text_view); ViewCompat.setElevation(this, elevation); initTextView(textAppearanceResId, text, hint); @@ -463,7 +467,7 @@ private void setDefaultMargins() { Resources resources = getResources(); int marginHorizontal = resources.getDimensionPixelSize(R.dimen.m3_searchbar_margin_horizontal); - int marginVertical = resources.getDimensionPixelSize(R.dimen.m3_searchbar_margin_vertical); + int marginVertical = resources.getDimensionPixelSize(getDefaultMarginVerticalResource()); MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams(); lp.leftMargin = defaultIfZero(lp.leftMargin, marginHorizontal); lp.topMargin = defaultIfZero(lp.topMargin, marginVertical); @@ -472,6 +476,20 @@ private void setDefaultMargins() { } } + /** @hide */ + @DimenRes + @RestrictTo(LIBRARY_GROUP) + protected int getDefaultMarginVerticalResource() { + return R.dimen.m3_searchbar_margin_vertical; + } + + /** @hide */ + @DrawableRes + @RestrictTo(LIBRARY_GROUP) + protected int getDefaultNavigationIconResource() { + return R.drawable.ic_search_black_24; + } + private int defaultIfZero(int value, int defValue) { return value == 0 ? defValue : value; } @@ -545,7 +563,7 @@ public TextView getTextView() { } /** Returns the text of main {@link TextView}, which usually represents the search text. */ - @Nullable + @NonNull // TextView.getText() never returns null after initialization. public CharSequence getText() { return textView.getText(); } diff --git a/lib/java/com/google/android/material/search/SearchView.java b/lib/java/com/google/android/material/search/SearchView.java index dcf753e14b9..1c2fbf509a4 100644 --- a/lib/java/com/google/android/material/search/SearchView.java +++ b/lib/java/com/google/android/material/search/SearchView.java @@ -48,6 +48,7 @@ import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.TextView; +import androidx.annotation.DrawableRes; import androidx.annotation.MenuRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -186,19 +187,19 @@ public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, int de LayoutInflater.from(context).inflate(R.layout.mtrl_search_view, this); layoutInflated = true; - scrim = findViewById(R.id.search_view_scrim); - rootView = findViewById(R.id.search_view_root); - backgroundView = findViewById(R.id.search_view_background); - statusBarSpacer = findViewById(R.id.search_view_status_bar_spacer); - headerContainer = findViewById(R.id.search_view_header_container); - toolbarContainer = findViewById(R.id.search_view_toolbar_container); - toolbar = findViewById(R.id.search_view_toolbar); - dummyToolbar = findViewById(R.id.search_view_dummy_toolbar); - searchPrefix = findViewById(R.id.search_view_search_prefix); - editText = findViewById(R.id.search_view_edit_text); - clearButton = findViewById(R.id.search_view_clear_button); - divider = findViewById(R.id.search_view_divider); - contentContainer = findViewById(R.id.search_view_content_container); + scrim = findViewById(R.id.open_search_view_scrim); + rootView = findViewById(R.id.open_search_view_root); + backgroundView = findViewById(R.id.open_search_view_background); + statusBarSpacer = findViewById(R.id.open_search_view_status_bar_spacer); + headerContainer = findViewById(R.id.open_search_view_header_container); + toolbarContainer = findViewById(R.id.open_search_view_toolbar_container); + toolbar = findViewById(R.id.open_search_view_toolbar); + dummyToolbar = findViewById(R.id.open_search_view_dummy_toolbar); + searchPrefix = findViewById(R.id.open_search_view_search_prefix); + editText = findViewById(R.id.open_search_view_edit_text); + clearButton = findViewById(R.id.open_search_view_clear_button); + divider = findViewById(R.id.open_search_view_divider); + contentContainer = findViewById(R.id.open_search_view_content_container); searchViewAnimationHelper = new SearchViewAnimationHelper(this); elevationOverlayProvider = new ElevationOverlayProvider(context); @@ -380,7 +381,7 @@ private void updateNavigationIconIfNeeded() { return; } - int navigationIcon = R.drawable.ic_arrow_back_black_24; + int navigationIcon = getDefaultNavigationIconResource(); if (searchBar == null) { toolbar.setNavigationIcon(navigationIcon); } else { @@ -623,7 +624,7 @@ public EditText getEditText() { /** Returns the text of main {@link EditText}, which usually represents the search text. */ @SuppressLint("KotlinPropertyAccess") // Editable extends CharSequence. - @Nullable + @NonNull // EditText never returns null after initialization. public Editable getText() { return editText.getText(); } @@ -883,6 +884,17 @@ private void updateChildImportantForAccessibility(ViewGroup parent, boolean isSe } } + /** + * Provides the resource identifier for the back arrow icon. + * + * @hide + */ + @DrawableRes + @RestrictTo(LIBRARY_GROUP) + protected int getDefaultNavigationIconResource() { + return R.drawable.ic_arrow_back_black_24; + } + /** Behavior that sets up an {@link SearchView} with an {@link SearchBar}. */ public static class Behavior extends CoordinatorLayout.Behavior { diff --git a/lib/java/com/google/android/material/search/res/layout/mtrl_search_bar.xml b/lib/java/com/google/android/material/search/res/layout/mtrl_search_bar.xml index c3b6dee3e71..dda3fd94b6d 100644 --- a/lib/java/com/google/android/material/search/res/layout/mtrl_search_bar.xml +++ b/lib/java/com/google/android/material/search/res/layout/mtrl_search_bar.xml @@ -17,7 +17,7 @@ + android:visibility="gone" + app:navigationIcon="@empty"/> + app:navigationContentDescription="@string/searchview_navigation_content_description"> diff --git a/lib/java/com/google/android/material/search/res/values/attrs.xml b/lib/java/com/google/android/material/search/res/values/attrs.xml index 3fdd691135d..bd3941ff677 100644 --- a/lib/java/com/google/android/material/search/res/values/attrs.xml +++ b/lib/java/com/google/android/material/search/res/values/attrs.xml @@ -22,8 +22,12 @@ + + + + diff --git a/lib/java/com/google/android/material/search/res/values/styles.xml b/lib/java/com/google/android/material/search/res/values/styles.xml index e3245fd36c9..a9ec70accaa 100644 --- a/lib/java/com/google/android/material/search/res/values/styles.xml +++ b/lib/java/com/google/android/material/search/res/values/styles.xml @@ -57,6 +57,7 @@ 0dp 0dp 0dp + @drawable/ic_arrow_back_black_24 ?attr/colorOnSurface @dimen/m3_searchview_height @dimen/m3_searchview_height diff --git a/lib/java/com/google/android/material/theme/res/values/themes_base.xml b/lib/java/com/google/android/material/theme/res/values/themes_base.xml index 324555c8c5c..88b5cfdb75b 100644 --- a/lib/java/com/google/android/material/theme/res/values/themes_base.xml +++ b/lib/java/com/google/android/material/theme/res/values/themes_base.xml @@ -161,6 +161,8 @@ @style/Widget.Material3.NavigationView @style/Widget.Material3.SearchBar @style/Widget.Material3.SearchView + @dimen/m3_searchview_height + @style/Widget.Material3.SearchView.Toolbar @style/Widget.Material3.CompoundButton.RadioButton @style/Widget.Material3.Slider @style/Widget.Material3.Snackbar @@ -422,6 +424,8 @@ @style/Widget.Material3.NavigationView @style/Widget.Material3.SearchBar @style/Widget.Material3.SearchView + @dimen/m3_searchview_height + @style/Widget.Material3.SearchView.Toolbar @style/Widget.Material3.CompoundButton.RadioButton @style/Widget.Material3.Slider @style/Widget.Material3.Snackbar