Skip to content

Commit e85371f

Browse files
imhappidsn5ft
authored andcommitted
[Searchbar] Update catalog demo to optionally show searchbar menu
PiperOrigin-RevId: 741210806
1 parent cd909c2 commit e85371f

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

catalog/java/io/material/catalog/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
android:windowSoftInputMode="adjustNothing"
112112
android:exported="true" />
113113
<activity
114-
android:name="io.material.catalog.search.SearchBarWithOutsideIconsDemoActivity"
114+
android:name="io.material.catalog.search.SearchBarWithAppBarIconsDemoActivity"
115115
android:windowSoftInputMode="adjustNothing"
116116
android:exported="true" />
117117
</application>

catalog/java/io/material/catalog/search/SearchBarWithOutsideIconsDemoActivity.java renamed to catalog/java/io/material/catalog/search/SearchBarWithAppBarIconsDemoActivity.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@
1818

1919
import io.material.catalog.R;
2020

21+
import static io.material.catalog.search.SearchDemoUtils.showSnackbar;
22+
2123
import android.os.Bundle;
2224
import android.view.LayoutInflater;
2325
import android.view.View;
2426
import android.view.ViewGroup;
2527
import android.widget.LinearLayout;
2628
import androidx.annotation.NonNull;
2729
import androidx.annotation.Nullable;
30+
import com.google.android.material.materialswitch.MaterialSwitch;
2831
import com.google.android.material.search.SearchBar;
2932
import com.google.android.material.search.SearchView;
3033
import io.material.catalog.feature.DemoActivity;
3134

32-
/** An activity that displays a Searchbar in an Appbar with outside icons demo. */
33-
public class SearchBarWithOutsideIconsDemoActivity extends DemoActivity {
35+
/** An activity that displays a SearchBar in an AppBar with outside icons demo. */
36+
public class SearchBarWithAppBarIconsDemoActivity extends DemoActivity {
3437

3538
@Nullable
3639
@Override
@@ -44,6 +47,19 @@ public View onCreateDemoView(
4447
SearchView searchView = view.findViewById(R.id.cat_search_view);
4548
LinearLayout suggestionContainer = view.findViewById(R.id.cat_search_view_suggestion_container);
4649

50+
MaterialSwitch menuSwitch = view.findViewById(R.id.cat_search_bar_menu_switch);
51+
menuSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
52+
if (isChecked) {
53+
searchBar.inflateMenu(R.menu.cat_searchview_menu);
54+
searchBar.setOnMenuItemClickListener(
55+
menuItem -> {
56+
showSnackbar(SearchBarWithAppBarIconsDemoActivity.this, menuItem);
57+
return true;
58+
});
59+
} else {
60+
searchBar.getMenu().clear();
61+
}
62+
});
4763
searchView.setupWithSearchBar(searchBar);
4864
SearchDemoUtils.setUpSearchView(this, searchBar, searchView);
4965
SearchDemoUtils.setUpSuggestions(suggestionContainer, searchBar, searchView);

catalog/java/io/material/catalog/search/SearchFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public Intent createActivityIntent() {
7070
}
7171
});
7272
additionalDemos.add(
73-
new Demo(R.string.cat_searchbar_outside_icon_title) {
73+
new Demo(R.string.cat_searchbar_appbar_with_icons_title) {
7474
@Override
7575
public Intent createActivityIntent() {
76-
return new Intent(getContext(), SearchBarWithOutsideIconsDemoActivity.class);
76+
return new Intent(getContext(), SearchBarWithAppBarIconsDemoActivity.class);
7777
}
7878
});
7979
return additionalDemos;

catalog/java/io/material/catalog/search/res/layout/cat_search_appbar_icons_fragment.xml

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,22 @@
2424
android:id="@+id/app_bar_layout"
2525
android:layout_width="match_parent"
2626
android:layout_height="wrap_content"
27-
android:fitsSystemWindows="true"
28-
app:statusBarForeground="?attr/colorSurface">
27+
android:fitsSystemWindows="true">
2928

3029
<LinearLayout
3130
android:layout_width="match_parent"
3231
android:layout_height="wrap_content"
3332
android:layout_gravity="center_vertical">
34-
<ImageView
33+
<Button
3534
android:id="@+id/product_logo"
3635
android:layout_width="48dp"
3736
android:layout_height="48dp"
38-
android:scaleType="center"
3937
android:layout_marginStart="4dp"
4038
android:layout_marginEnd="8dp"
4139
android:contentDescription="@string/cat_searchbar_home_icon_description"
40+
style="?attr/materialIconButtonStyle"
4241
android:layout_gravity="center_vertical"
43-
android:src="@drawable/ic_home_default_24px"/>
42+
app:icon="@drawable/ic_home_default_24px"/>
4443
<com.google.android.material.search.SearchBar
4544
android:id="@+id/cat_search_bar"
4645
android:layout_width="0dp"
@@ -49,18 +48,16 @@
4948
android:layout_marginVertical="4dp"
5049
android:layout_gravity="center_vertical"
5150
app:defaultMarginsEnabled="false"
52-
app:textCentered="true"
53-
app:hideNavigationIcon="true"
5451
android:hint="@string/cat_searchbar_short_hint"/>
55-
<ImageView
52+
<Button
5653
android:layout_width="48dp"
5754
android:layout_height="48dp"
58-
android:scaleType="center"
5955
android:layout_marginStart="8dp"
6056
android:layout_marginEnd="4dp"
57+
style="?attr/materialIconButtonStyle"
6158
android:contentDescription="@string/cat_searchbar_cast_icon_description"
6259
android:layout_gravity="center_vertical"
63-
android:src="@drawable/ic_cast_vd_theme_24" />
60+
app:icon="@drawable/ic_cast_vd_theme_24" />
6461
</LinearLayout>
6562

6663
</com.google.android.material.appbar.AppBarLayout>
@@ -71,13 +68,35 @@
7168
android:layout_height="match_parent"
7269
app:layout_behavior="@string/appbar_scrolling_view_behavior">
7370

71+
<LinearLayout
72+
android:layout_width="match_parent"
73+
android:layout_height="match_parent"
74+
android:orientation="vertical">
75+
<LinearLayout
76+
android:layout_width="wrap_content"
77+
android:layout_height="wrap_content"
78+
android:layout_gravity="center"
79+
android:orientation="horizontal">
80+
<TextView
81+
android:layout_width="wrap_content"
82+
android:layout_height="wrap_content"
83+
android:layout_gravity="center"
84+
android:text="@string/cat_searchbar_menu_switch_title"/>
85+
<com.google.android.material.materialswitch.MaterialSwitch
86+
android:id="@+id/cat_search_bar_menu_switch"
87+
android:layout_gravity="center_horizontal"
88+
android:layout_width="wrap_content"
89+
android:layout_height="wrap_content"
90+
android:padding="24dp" />
91+
</LinearLayout>
92+
7493
<TextView
7594
android:layout_width="match_parent"
7695
android:layout_height="wrap_content"
7796
android:layout_margin="16dp"
78-
android:fitsSystemWindows="true"
7997
android:lineSpacingMultiplier="1.2"
8098
android:text="@string/cat_searchbar_lorem_ipsum"/>
99+
</LinearLayout>
81100
</androidx.core.widget.NestedScrollView>
82101

83102
<com.google.android.material.search.SearchView

catalog/java/io/material/catalog/search/res/values/strings.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
description="Title for trips tab.[CHAR_LIMIT=40]">Trips</string>
7070
<string name="cat_searchbar_recycler_title"
7171
description="Title for recycler view demo.[CHAR_LIMIT=100]">RecyclerView Demo</string>
72-
<string name="cat_searchbar_outside_icon_title"
73-
description="Title for Searchbar with outside icons demo.[CHAR_LIMIT=NONE]">Searchbar Demo with Outside Icons</string>
72+
<string name="cat_searchbar_appbar_with_icons_title"
73+
description="Title for SearchBar in an AppBarLayout with icons demo.[CHAR_LIMIT=NONE]">Searchar in AppBarLayout with Icons Demo</string>
7474
<string name="cat_searchbar_recycler_item_title_prefix"
7575
description="Title for recycler item.[CHAR_LIMIT=40]">Message</string>
7676

@@ -94,4 +94,6 @@
9494
description="Description about a cast icon button.[CHAR_LIMIT=NONE]">Cast</string>
9595
<string name="cat_searchbar_short_hint"
9696
description="Search bar short hint text to open search view [CHAR_LIMIT=40]">Search product</string>
97+
<string name="cat_searchbar_menu_switch_title"
98+
description="Title of a switch that enables or disables a menu inside of the SearchBar [CHAR_LIMIT=NONE]">Enable menu within SearchBar</string>
9799
</resources>

0 commit comments

Comments
 (0)