Skip to content

Commit 8726702

Browse files
pekingmeimhappi
authored andcommitted
[TopAppBar] Added a demo to show case using collapsing toolbar layout with a toggleable button; reduced the number of menu items in all demos.
PiperOrigin-RevId: 708397663
1 parent 9496619 commit 8726702

6 files changed

+153
-8
lines changed

catalog/java/io/material/catalog/topappbar/TopAppBarCollapsingFilledActionDemoFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import io.material.catalog.feature.DemoFragment;
3232

3333
/**
34-
* A fragment that displays a large Collapsing Toolbar Top App Bar with a filled action button demo
34+
* A fragment that displays a medium Collapsing Toolbar Top App Bar with a filled action button demo
3535
* for the Catalog app.
3636
*/
3737
public class TopAppBarCollapsingFilledActionDemoFragment extends DemoFragment {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.material.catalog.topappbar;
18+
19+
import io.material.catalog.R;
20+
21+
import android.os.Bundle;
22+
import androidx.appcompat.app.AppCompatActivity;
23+
import androidx.appcompat.widget.Toolbar;
24+
import android.view.LayoutInflater;
25+
import android.view.View;
26+
import android.view.ViewGroup;
27+
import androidx.annotation.NonNull;
28+
import androidx.annotation.Nullable;
29+
import com.google.android.material.button.MaterialButton;
30+
import com.google.android.material.snackbar.Snackbar;
31+
import io.material.catalog.feature.DemoFragment;
32+
33+
/**
34+
* A fragment that displays a medium Collapsing Toolbar Top App Bar with a toggleable action button
35+
* demo for the Catalog app.
36+
*/
37+
public class TopAppBarCollapsingToggleableActionDemoFragment extends DemoFragment {
38+
39+
@Override
40+
@Nullable
41+
public View onCreateDemoView(
42+
@NonNull LayoutInflater layoutInflater,
43+
@Nullable ViewGroup viewGroup,
44+
@Nullable Bundle bundle) {
45+
View view =
46+
layoutInflater.inflate(
47+
R.layout.cat_topappbar_collapsing_toggleable_action_fragment, viewGroup, false);
48+
49+
Toolbar toolbar = view.findViewById(R.id.toolbar);
50+
AppCompatActivity activity = (AppCompatActivity) getActivity();
51+
activity.setSupportActionBar(toolbar);
52+
53+
MaterialButton actionButton = view.findViewById(R.id.action_button);
54+
actionButton.setOnClickListener(
55+
v ->
56+
Snackbar.make(
57+
v,
58+
(actionButton.isChecked() ? "Marked as favorite." : "Marked as not favorite"),
59+
Snackbar.LENGTH_SHORT)
60+
.show());
61+
62+
return view;
63+
}
64+
65+
@Override
66+
public boolean shouldShowDefaultDemoActionBar() {
67+
return false;
68+
}
69+
}

catalog/java/io/material/catalog/topappbar/TopAppBarFragment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ public Fragment createFragment() {
139139
public Fragment createFragment() {
140140
return new TopAppBarCollapsingFilledActionDemoFragment();
141141
}
142+
},
143+
new Demo(R.string.cat_topappbar_collapsing_with_toggleable_action_demo_title) {
144+
@Nullable
145+
@Override
146+
public Fragment createFragment() {
147+
return new TopAppBarCollapsingToggleableActionDemoFragment();
148+
}
142149
});
143150
}
144151

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2024 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<androidx.coordinatorlayout.widget.CoordinatorLayout
18+
xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:id="@+id/coordinator"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent">
23+
24+
<com.google.android.material.appbar.AppBarLayout
25+
android:id="@+id/appbarlayout"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
android:fitsSystemWindows="true">
29+
30+
<com.google.android.material.appbar.CollapsingToolbarLayout
31+
style="?attr/collapsingToolbarLayoutMediumStyle"
32+
android:id="@+id/collapsingtoolbarlayout"
33+
android:layout_width="match_parent"
34+
android:layout_height="?attr/collapsingToolbarLayoutMediumWithSubtitleSize"
35+
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
36+
app:expandedTitleGravity="bottom|center_horizontal"
37+
app:title="@string/cat_topappbar_collapsing_demo_toolbar_medium_title"
38+
app:subtitle="@string/cat_topappbar_collapsing_demo_toolbar_medium_subtitle"
39+
app:toolbarId="@id/toolbar">
40+
<LinearLayout
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content"
43+
android:orientation="horizontal"
44+
app:layout_collapseMode="pin">
45+
<androidx.appcompat.widget.Toolbar
46+
android:id="@+id/toolbar"
47+
style="?attr/catalogToolbarWithCloseButtonStyle"
48+
android:layout_width="0dp"
49+
android:layout_height="?attr/actionBarSize"
50+
android:layout_weight="1"
51+
android:elevation="0dp"/>
52+
<Button
53+
android:id="@+id/action_button"
54+
style="?attr/materialIconButtonFilledStyle"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:layout_marginEnd="4dp"
58+
android:layout_gravity="center_vertical"
59+
android:checkable="true"
60+
app:icon="@drawable/ic_favorite_checkable_24px"/>
61+
</LinearLayout>
62+
</com.google.android.material.appbar.CollapsingToolbarLayout>
63+
</com.google.android.material.appbar.AppBarLayout>
64+
65+
<androidx.core.widget.NestedScrollView
66+
android:layout_width="match_parent"
67+
android:layout_height="match_parent"
68+
app:layout_behavior="@string/appbar_scrolling_view_behavior">
69+
70+
<include layout="@layout/cat_topappbar_filler_text_view"/>
71+
</androidx.core.widget.NestedScrollView>
72+
73+
</androidx.coordinatorlayout.widget.CoordinatorLayout>

catalog/java/io/material/catalog/topappbar/res/menu/cat_topappbar_menu.xml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@
1919
xmlns:tools="http://schemas.android.com/tools"
2020
tools:ignore="AppCompatResource">
2121
<item
22-
android:id="@+id/cat_topappbar_item_edit"
22+
android:id="@+id/cat_topappbar_item_edit"
23+
android:icon="@drawable/ic_edit_vd_theme_24dp"
2324
android:title="@string/cat_topappbar_edit_menu_item_title"
2425
app:showAsAction="ifRoom"/>
2526
<item
2627
android:id="@+id/cat_topappbar_item_favorite"
2728
android:icon="@drawable/ic_favorite_vd_theme_24px"
2829
android:title="@string/cat_topappbar_favorite_menu_item_title"
2930
app:showAsAction="ifRoom"/>
30-
<item
31-
android:title="@string/cat_topappbar_settings_menu_item_title"
32-
app:showAsAction="never"/>
33-
<item
34-
android:title="@string/cat_topappbar_help_and_feedback_menu_item_title"
35-
app:showAsAction="never"/>
3631
</menu>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<string name="cat_topappbar_collapsing_large_demo_title" translatable="false">Collapsing Demo (Large)</string>
2828
<string name="cat_topappbar_collapsing_large_with_subtitle_demo_title" translatable="false">Collapsing with Subtitle Demo (Large)</string>
2929
<string name="cat_topappbar_collapsing_with_filled_action_demo_title" translatable="false">Collapsing Demo (Filled Action)</string>
30+
<string name="cat_topappbar_collapsing_with_toggleable_action_demo_title" translatable="false">Collapsing Demo (Toggleable Action)</string>
3031
<string name="cat_topappbar_collapsing_multiline_demo_title" translatable="false">Collapsing Demo (Multiline title)</string>
3132
<string name="cat_topappbar_toolbar_title" translatable="false">Toolbar Demo</string>
3233
<string name="cat_topappbar_action_bar_title" translatable="false">Action Bar Demo</string>

0 commit comments

Comments
 (0)