Skip to content

Commit

Permalink
[M3][SearchBar][SearchView] Updated search bar/view UI to match spec …
Browse files Browse the repository at this point in the history
…and cleaned up the API.

PiperOrigin-RevId: 492278545
(cherry picked from commit d0b616c)
  • Loading branch information
raajkumars authored and dsn5ft committed Dec 9, 2022
1 parent e6ca949 commit d049b1b
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 86 deletions.
72 changes: 40 additions & 32 deletions lib/java/com/google/android/material/search/SearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,41 @@
* methods, or their corresponding xml attributes. Instead, use {@link #setHint} or {@link
* #setText}, or their corresponding xml attributes, to provide a text affordance for your {@link
* SearchBar}.
*
* <p>The example below shows how to use the {@link SearchBar} and {@link SearchView} together:
*
* <pre>
* &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;match_parent&quot;&gt;
*
* &lt;!-- NestedScrollingChild goes here (NestedScrollView, RecyclerView, etc.). --&gt;
* &lt;androidx.core.widget.NestedScrollView
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;match_parent&quot;
* app:layout_behavior=&quot;@string/searchbar_scrolling_view_behavior&quot;&gt;
* &lt;!-- Screen content goes here. --&gt;
* &lt;/androidx.core.widget.NestedScrollView&gt;
*
* &lt;com.google.android.material.appbar.AppBarLayout
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;wrap_content&quot;&gt;
* &lt;com.google.android.material.search.SearchBar
* android:id=&quot;@+id/search_bar&quot;
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;wrap_content&quot;
* android:hint=&quot;@string/searchbar_hint&quot; /&gt;
* &lt;/com.google.android.material.appbar.AppBarLayout&gt;
*
* &lt;com.google.android.material.search.SearchView
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;match_parent&quot;
* android:hint=&quot;@string/searchbar_hint&quot;
* app:layout_anchor=&quot;@id/search_bar&quot;&gt;
* &lt;!-- Search suggestions/results go here (ScrollView, RecyclerView, etc.). --&gt;
* &lt;/com.google.android.material.search.SearchView&gt;
* &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
* </pre>
*/
public class SearchBar extends Toolbar {

Expand Down Expand Up @@ -457,10 +492,7 @@ public View getCenterView() {
return centerView;
}

/**
* Sets and adds the center view as a child. If you've set a center view and would like to remove
* it, pass in null for {@param view}.
*/
/** Sets the center view as a child. Pass in null for {@code view} to remove the center view. */
public void setCenterView(@Nullable View view) {
if (centerView != null) {
removeView(centerView);
Expand Down Expand Up @@ -594,26 +626,18 @@ public void setOnLoadAnimationFadeInEnabled(boolean onLoadAnimationFadeInEnabled
* Registers a callback for the On Load Animation, started and stopped via {@link
* #startOnLoadAnimation()} and {@link #stopOnLoadAnimation()}.
*/
public void registerOnLoadAnimationCallback(
public void addOnLoadAnimationCallback(
@NonNull OnLoadAnimationCallback onLoadAnimationCallback) {
searchBarAnimationHelper.registerOnLoadAnimationCallback(onLoadAnimationCallback);
searchBarAnimationHelper.addOnLoadAnimationCallback(onLoadAnimationCallback);
}

/**
* Unregisters a callback for the On Load Animation, started and stopped via {@link
* #startOnLoadAnimation()} and {@link #stopOnLoadAnimation()}.
*/
public boolean unregisterOnLoadAnimationCallback(
public boolean removeOnLoadAnimationCallback(
@NonNull OnLoadAnimationCallback onLoadAnimationCallback) {
return searchBarAnimationHelper.unregisterOnLoadAnimationCallback(onLoadAnimationCallback);
}

/**
* Clears all registered callbacks for the On Load Animation, started and stopped via {@link
* #startOnLoadAnimation()} and {@link #stopOnLoadAnimation()}.
*/
public void clearOnLoadAnimationCallbacks() {
searchBarAnimationHelper.clearOnLoadAnimationCallbacks();
return searchBarAnimationHelper.removeOnLoadAnimationCallback(onLoadAnimationCallback);
}

/** Returns whether the expand animation is running. */
Expand Down Expand Up @@ -674,14 +698,6 @@ public boolean removeExpandAnimationListener(@NonNull AnimatorListenerAdapter li
return searchBarAnimationHelper.removeExpandAnimationListener(listener);
}

/**
* Removes all expand animation listeners added via {@link
* #addExpandAnimationListener(AnimatorListenerAdapter)}.
*/
public void clearExpandAnimationListeners() {
searchBarAnimationHelper.clearExpandAnimationListeners();
}

/** Returns whether the collapse animation is running. */
public boolean isCollapsing() {
return searchBarAnimationHelper.isCollapsing();
Expand Down Expand Up @@ -740,14 +756,6 @@ public boolean removeCollapseAnimationListener(@NonNull AnimatorListenerAdapter
return searchBarAnimationHelper.removeCollapseAnimationListener(listener);
}

/**
* Removes all collapse animation listeners added via {@link
* #addCollapseAnimationListener(AnimatorListenerAdapter)}.
*/
public void clearCollapseAnimationListeners() {
searchBarAnimationHelper.clearCollapseAnimationListeners();
}

int getMenuResId() {
return menuResId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,14 @@ void setOnLoadAnimationFadeInEnabled(boolean onLoadAnimationFadeInEnabled) {
this.onLoadAnimationFadeInEnabled = onLoadAnimationFadeInEnabled;
}

void registerOnLoadAnimationCallback(OnLoadAnimationCallback onLoadAnimationCallback) {
void addOnLoadAnimationCallback(OnLoadAnimationCallback onLoadAnimationCallback) {
onLoadAnimationCallbacks.add(onLoadAnimationCallback);
}

boolean unregisterOnLoadAnimationCallback(OnLoadAnimationCallback onLoadAnimationCallback) {
boolean removeOnLoadAnimationCallback(OnLoadAnimationCallback onLoadAnimationCallback) {
return onLoadAnimationCallbacks.remove(onLoadAnimationCallback);
}

void clearOnLoadAnimationCallbacks() {
onLoadAnimationCallbacks.clear();
}

private void dispatchOnLoadAnimation(OnLoadAnimationInvocation invocation) {
for (OnLoadAnimationCallback onLoadAnimationCallback : onLoadAnimationCallbacks) {
invocation.invoke(onLoadAnimationCallback);
Expand Down Expand Up @@ -273,10 +269,6 @@ boolean removeExpandAnimationListener(@NonNull AnimatorListenerAdapter listener)
return expandAnimationListeners.remove(listener);
}

void clearExpandAnimationListeners() {
expandAnimationListeners.clear();
}

void startCollapseAnimation(
SearchBar searchBar,
View expandedView,
Expand Down Expand Up @@ -344,10 +336,6 @@ boolean removeCollapseAnimationListener(@NonNull AnimatorListenerAdapter listene
return collapseAnimationListeners.remove(listener);
}

void clearCollapseAnimationListeners() {
collapseAnimationListeners.clear();
}

private ExpandCollapseAnimationHelper getExpandCollapseAnimationHelper(
SearchBar searchBar, View expandedView, @Nullable AppBarLayout appBarLayout) {
return new ExpandCollapseAnimationHelper(searchBar, expandedView)
Expand Down
97 changes: 74 additions & 23 deletions lib/java/com/google/android/material/search/SearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.annotation.SuppressLint;
Expand Down Expand Up @@ -52,10 +53,10 @@
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.graphics.ColorUtils;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
Expand All @@ -77,7 +78,45 @@
import java.util.Map;
import java.util.Set;

/** Layout that provides a full screen search view and can be used with {@link SearchBar}. */
/**
* Layout that provides a full screen search view and can be used with {@link SearchBar}.
*
*
* <p>The example below shows how to use the {@link SearchBar} and {@link SearchView} together:
*
* <pre>
* &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;match_parent&quot;&gt;
*
* &lt;!-- NestedScrollingChild goes here (NestedScrollView, RecyclerView, etc.). --&gt;
* &lt;androidx.core.widget.NestedScrollView
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;match_parent&quot;
* app:layout_behavior=&quot;@string/searchbar_scrolling_view_behavior&quot;&gt;
* &lt;!-- Screen content goes here. --&gt;
* &lt;/androidx.core.widget.NestedScrollView&gt;
*
* &lt;com.google.android.material.appbar.AppBarLayout
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;wrap_content&quot;&gt;
* &lt;com.google.android.material.search.SearchBar
* android:id=&quot;@+id/search_bar&quot;
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;wrap_content&quot;
* android:hint=&quot;@string/searchbar_hint&quot; /&gt;
* &lt;/com.google.android.material.appbar.AppBarLayout&gt;
*
* &lt;com.google.android.material.search.SearchView
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;match_parent&quot;
* android:hint=&quot;@string/searchbar_hint&quot;
* app:layout_anchor=&quot;@id/search_bar&quot;&gt;
* &lt;!-- Search suggestions/results go here (ScrollView, RecyclerView, etc.). --&gt;
* &lt;/com.google.android.material.search.SearchView&gt;
* &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
* </pre>
*/
@SuppressWarnings("RestrictTo")
public class SearchView extends FrameLayout implements CoordinatorLayout.AttachedBehavior {

Expand Down Expand Up @@ -169,7 +208,6 @@ public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, int de
setUpEditText(textAppearanceResId, text, hint);
setUpBackButton(useDrawerArrowDrawable, hideNavigationIcon);
setUpClearButton();
setUpDivider();
setUpContentOnTouchListener();
setUpInsetListeners();
}
Expand All @@ -187,10 +225,9 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
protected void onFinishInflate() {
super.onFinishInflate();

Activity activity = ContextUtils.getActivity(getContext());
if (activity != null) {
Window window = activity.getWindow();
setSoftInputMode(window);
Window window = getActivityWindow();
if (window != null) {
this.softInputMode = window.getAttributes().softInputMode;
setStatusBarSpacerEnabled(shouldShowStatusBarSpacer(window));
}
}
Expand All @@ -215,6 +252,12 @@ public CoordinatorLayout.Behavior<SearchView> getBehavior() {
return new SearchView.Behavior();
}

@Nullable
private Window getActivityWindow() {
Activity activity = ContextUtils.getActivity(getContext());
return activity == null ? null : activity.getWindow();
}

@SuppressLint("ClickableViewAccessibility") // Will be handled by accessibility delegate.
private void setUpRootView() {
rootView.setOnTouchListener((v, event) -> true);
Expand Down Expand Up @@ -294,12 +337,6 @@ public void afterTextChanged(Editable s) {}
});
}

private void setUpDivider() {
int colorOnSurface = MaterialColors.getColor(this, R.attr.colorOnSurface);
int dividerColor = ColorUtils.setAlphaComponent(colorOnSurface, Math.round(0.12f * 255));
divider.setBackgroundColor(dividerColor);
}

@SuppressLint("ClickableViewAccessibility") // Will be handled by accessibility delegate.
private void setUpContentOnTouchListener() {
contentContainer.setOnTouchListener(
Expand Down Expand Up @@ -423,7 +460,7 @@ private void setUpDividerInsetListener() {
}

/** Returns whether or not this {@link SearchView} is set up with an {@link SearchBar}. */
public boolean isSetUpWithSearchBar() {
public boolean isSetupWithSearchBar() {
return this.searchBar != null;
}

Expand All @@ -433,7 +470,7 @@ public boolean isSetUpWithSearchBar() {
* automatically if the {@link SearchBar} and {@link SearchView} are in a {@link
* CoordinatorLayout} and the {@link SearchView} is anchored to the {@link SearchBar}.
*/
public void setUpWithSearchBar(@Nullable SearchBar searchBar) {
public void setupWithSearchBar(@Nullable SearchBar searchBar) {
this.searchBar = searchBar;
searchViewAnimationHelper.setSearchBar(searchBar);
if (searchBar != null) {
Expand Down Expand Up @@ -489,15 +526,15 @@ public boolean isAnimatedNavigationIcon() {
* Sets whether the menu items should be animated from the {@link SearchBar} to {@link
* SearchView}.
*/
public void setAnimatedMenuItems(boolean animatedMenuItems) {
this.animatedMenuItems = animatedMenuItems;
public void setMenuItemsAnimated(boolean menuItemsAnimated) {
this.animatedMenuItems = menuItemsAnimated;
}

/**
* Returns whether the menu items should be animated from the {@link SearchBar} to {@link
* SearchView}.
*/
public boolean isAnimatedMenuItems() {
public boolean isMenuItemsAnimated() {
return animatedMenuItems;
}

Expand All @@ -511,12 +548,22 @@ public boolean isAutoShowKeyboard() {
return autoShowKeyboard;
}

/** Sets whether the soft keyboard should be shown with {@code WindowInsetsController}. */
/**
* Sets whether the soft keyboard should be shown with {@code WindowInsetsController}.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public void setUseWindowInsetsController(boolean useWindowInsetsController) {
this.useWindowInsetsController = useWindowInsetsController;
}

/** Returns whether the soft keyboard should be shown with {@code WindowInsetsController}. */
/**
* Returns whether the soft keyboard should be shown with {@code WindowInsetsController}.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public boolean isUseWindowInsetsController() {
return useWindowInsetsController;
}
Expand Down Expand Up @@ -625,7 +672,8 @@ public int getSoftInputMode() {
* the {@link SearchView} during initial render but make sure to invoke this if you are changing
* the soft input mode at runtime.
*/
public void setSoftInputMode(@Nullable Window window) {
public void updateSoftInputMode() {
Window window = getActivityWindow();
if (window != null) {
this.softInputMode = window.getAttributes().softInputMode;
}
Expand All @@ -637,7 +685,10 @@ public void setSoftInputMode(@Nullable Window window) {
* automatically by the {@link SearchView} during initial render based on {@link
* #shouldShowStatusBarSpacer(Window)}, but make sure to invoke this if you would like to override
* the default behavior.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public void setStatusBarSpacerEnabled(boolean enabled) {
statusBarSpacer.setVisibility(enabled ? VISIBLE : GONE);
}
Expand Down Expand Up @@ -846,8 +897,8 @@ public Behavior(@NonNull Context context, @Nullable AttributeSet attrs) {
@Override
public boolean onDependentViewChanged(
@NonNull CoordinatorLayout parent, @NonNull SearchView child, @NonNull View dependency) {
if (!child.isSetUpWithSearchBar() && dependency instanceof SearchBar) {
child.setUpWithSearchBar((SearchBar) dependency);
if (!child.isSetupWithSearchBar() && dependency instanceof SearchBar) {
child.setupWithSearchBar((SearchBar) dependency);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private void setContentViewsAlpha(float alpha) {
}

private void setActionMenuViewAlphaIfNeeded(float alpha) {
if (searchView.isAnimatedMenuItems()) {
if (searchView.isMenuItemsAnimated()) {
ActionMenuView actionMenuView = ToolbarUtils.getActionMenuView(toolbar);
if (actionMenuView != null) {
actionMenuView.setAlpha(alpha);
Expand Down Expand Up @@ -468,7 +468,7 @@ private Animator getActionMenuViewsAlphaAnimator(boolean show) {
animator.setInterpolator(
ReversableAnimatedValueInterpolator.of(show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));

if (searchView.isAnimatedMenuItems()) {
if (searchView.isMenuItemsAnimated()) {
ActionMenuView dummyActionMenuView = ToolbarUtils.getActionMenuView(dummyToolbar);
ActionMenuView actionMenuView = ToolbarUtils.getActionMenuView(toolbar);
animator.addUpdateListener(
Expand Down Expand Up @@ -573,7 +573,7 @@ private void setUpDummyToolbarIfNeeded() {
if (menu != null) {
menu.clear();
}
if (searchBar.getMenuResId() != -1 && searchView.isAnimatedMenuItems()) {
if (searchBar.getMenuResId() != -1 && searchView.isMenuItemsAnimated()) {
dummyToolbar.inflateMenu(searchBar.getMenuResId());
setMenuItemsNotClickable(dummyToolbar);
dummyToolbar.setVisibility(View.VISIBLE);
Expand Down
Loading

0 comments on commit d049b1b

Please sign in to comment.