Skip to content

Commit 655dba1

Browse files
afohrmandrchen
authored andcommitted
[FloatingToolbar] Added vertical floating toolbar configuration.
PiperOrigin-RevId: 694362191
1 parent 9c1ae33 commit 655dba1

File tree

6 files changed

+286
-109
lines changed

6 files changed

+286
-109
lines changed

catalog/java/io/material/catalog/floatingtoolbar/FloatingToolbarMainDemoFragment.java

Lines changed: 126 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@
2626
import android.view.ViewGroup;
2727
import android.widget.TextView;
2828
import androidx.annotation.ColorInt;
29+
import androidx.annotation.IdRes;
2930
import androidx.annotation.NonNull;
3031
import androidx.annotation.Nullable;
3132
import com.google.android.material.button.MaterialButton;
33+
import com.google.android.material.floatingtoolbar.FloatingToolbarLayout;
3234
import io.material.catalog.feature.DemoFragment;
35+
import io.material.catalog.feature.DemoUtils;
36+
import java.util.ArrayList;
37+
import java.util.List;
3338
import java.util.Random;
3439

3540
/** A fragment that displays the main Floating Toolbar demo for the Catalog app. */
@@ -46,62 +51,131 @@ public View onCreateDemoView(
4651
layoutInflater.inflate(
4752
R.layout.cat_floating_toolbar_fragment, viewGroup, /* attachToRoot= */ false);
4853

49-
TextView bodyText = (TextView) view.findViewById(R.id.body_text);
50-
51-
// Initialize bold format button.
52-
MaterialButton boldButton = view.findViewById(R.id.floating_toolbar_button_bold);
53-
boldButton.addOnCheckedChangeListener(
54-
(button, isChecked) -> {
55-
Typeface typeface = bodyText.getTypeface();
56-
if (isChecked) {
57-
bodyText.setTypeface(
58-
typeface, typeface.isItalic() ? Typeface.BOLD_ITALIC : Typeface.BOLD);
59-
} else {
60-
bodyText.setTypeface(
61-
Typeface.create(
62-
bodyText.getTypeface(),
63-
typeface.isItalic() ? Typeface.ITALIC : Typeface.NORMAL));
64-
}
65-
});
66-
67-
// Initialize italics format button.
68-
MaterialButton italicsButton = view.findViewById(R.id.floating_toolbar_button_italic);
69-
italicsButton.addOnCheckedChangeListener(
70-
(button, isChecked) -> {
71-
Typeface typeface = bodyText.getTypeface();
72-
if (isChecked) {
73-
bodyText.setTypeface(
74-
typeface, typeface.isBold() ? Typeface.BOLD_ITALIC : Typeface.ITALIC);
75-
} else {
76-
bodyText.setTypeface(
77-
Typeface.create(
78-
bodyText.getTypeface(), typeface.isBold() ? Typeface.BOLD : Typeface.NORMAL));
79-
}
80-
});
81-
82-
// Initialize underlined format button.
83-
MaterialButton underlinedButton = view.findViewById(R.id.floating_toolbar_button_underlined);
84-
underlinedButton.addOnCheckedChangeListener(
85-
(button, isChecked) -> {
86-
int paintFlags = bodyText.getPaintFlags();
87-
if (isChecked) {
88-
bodyText.setPaintFlags(paintFlags | Paint.UNDERLINE_TEXT_FLAG);
89-
} else {
90-
bodyText.setPaintFlags(paintFlags & ~Paint.UNDERLINE_TEXT_FLAG);
91-
}
92-
});
93-
94-
// Initialize color text format button.
95-
MaterialButton colorTextButton = view.findViewById(R.id.floating_toolbar_button_color_text);
96-
colorTextButton.setOnClickListener(v -> bodyText.setTextColor(getRandomColor()));
97-
98-
// Initialize color fill format button.
99-
MaterialButton colorFillButton = view.findViewById(R.id.floating_toolbar_button_color_fill);
100-
colorFillButton.setOnClickListener(v -> view.setBackgroundColor(getRandomColor()));
54+
TextView bodyText = view.findViewById(R.id.body_text);
55+
56+
// Initialize group of floating toolbars.
57+
List<FloatingToolbarLayout> floatingToolbars =
58+
DemoUtils.findViewsWithType(view, FloatingToolbarLayout.class);
59+
60+
// Initialize group of bold buttons.
61+
List<MaterialButton> boldButtons =
62+
initializeFormatButtons(floatingToolbars, R.id.floating_toolbar_button_bold);
63+
for (MaterialButton boldButton : boldButtons) {
64+
boldButton.addOnCheckedChangeListener(
65+
(button, isChecked) -> {
66+
Typeface typeface = bodyText.getTypeface();
67+
if (isChecked) {
68+
bodyText.setTypeface(
69+
typeface, typeface.isItalic() ? Typeface.BOLD_ITALIC : Typeface.BOLD);
70+
} else {
71+
bodyText.setTypeface(
72+
Typeface.create(
73+
bodyText.getTypeface(),
74+
typeface.isItalic() ? Typeface.ITALIC : Typeface.NORMAL));
75+
}
76+
propagateCheckedButtonState(boldButtons, isChecked);
77+
});
78+
}
79+
80+
// Initialize group of italics format buttons.
81+
List<MaterialButton> italicButtons =
82+
initializeFormatButtons(floatingToolbars, R.id.floating_toolbar_button_italic);
83+
for (MaterialButton italicButton : italicButtons) {
84+
italicButton.addOnCheckedChangeListener(
85+
(button, isChecked) -> {
86+
Typeface typeface = bodyText.getTypeface();
87+
if (isChecked) {
88+
bodyText.setTypeface(
89+
typeface, typeface.isBold() ? Typeface.BOLD_ITALIC : Typeface.ITALIC);
90+
} else {
91+
bodyText.setTypeface(
92+
Typeface.create(
93+
bodyText.getTypeface(), typeface.isBold() ? Typeface.BOLD : Typeface.NORMAL));
94+
}
95+
propagateCheckedButtonState(italicButtons, isChecked);
96+
});
97+
}
98+
99+
// Initialize group of underline format buttons.
100+
List<MaterialButton> underlineButtons =
101+
initializeFormatButtons(floatingToolbars, R.id.floating_toolbar_button_underlined);
102+
for (MaterialButton underlineButton : underlineButtons) {
103+
underlineButton.addOnCheckedChangeListener(
104+
(button, isChecked) -> {
105+
int paintFlags = bodyText.getPaintFlags();
106+
if (isChecked) {
107+
bodyText.setPaintFlags(paintFlags | Paint.UNDERLINE_TEXT_FLAG);
108+
} else {
109+
bodyText.setPaintFlags(paintFlags & ~Paint.UNDERLINE_TEXT_FLAG);
110+
}
111+
propagateCheckedButtonState(underlineButtons, isChecked);
112+
});
113+
}
114+
115+
// Initialize color text format buttons.
116+
List<MaterialButton> colorTextButtons =
117+
initializeFormatButtons(floatingToolbars, R.id.floating_toolbar_button_color_text);
118+
for (MaterialButton colorTextButton : colorTextButtons) {
119+
colorTextButton.setOnClickListener(v -> bodyText.setTextColor(getRandomColor()));
120+
}
121+
122+
// Initialize color fill format buttons.
123+
List<MaterialButton> colorFillButtons =
124+
initializeFormatButtons(floatingToolbars, R.id.floating_toolbar_button_color_fill);
125+
for (MaterialButton colorFillButton : colorFillButtons) {
126+
colorFillButton.setOnClickListener(v -> view.setBackgroundColor(getRandomColor()));
127+
}
128+
129+
// Initialize orientation configuration selection controls.
130+
initializeOrientationButton(
131+
view, floatingToolbars, R.id.bottom_button, R.id.floating_toolbar_bottom);
132+
initializeOrientationButton(
133+
view, floatingToolbars, R.id.left_button, R.id.floating_toolbar_left);
134+
initializeOrientationButton(view, floatingToolbars, R.id.right_button, R.id.floating_toolbar_right);
135+
136+
// Select bottom configuration button to represent the toolbar that's initially visible.
137+
view.findViewById(R.id.bottom_button).performClick();
101138

102139
return view;
103140
}
104141

142+
private void initializeOrientationButton(
143+
@NonNull View view,
144+
@NonNull List<FloatingToolbarLayout> floatingToolbars,
145+
@IdRes int buttonId,
146+
@IdRes int floatingToolbarId) {
147+
MaterialButton orientationButton = view.findViewById(buttonId);
148+
orientationButton.setOnClickListener(
149+
v -> updateFloatingToolbarVisibilities(floatingToolbars, floatingToolbarId));
150+
}
151+
152+
private void updateFloatingToolbarVisibilities(
153+
@NonNull List<FloatingToolbarLayout> floatingToolbars, @IdRes int visibleFloatingToolbarId) {
154+
for (FloatingToolbarLayout floatingToolbar : floatingToolbars) {
155+
if (floatingToolbar.getId() == visibleFloatingToolbarId) {
156+
floatingToolbar.setVisibility(View.VISIBLE);
157+
} else {
158+
floatingToolbar.setVisibility(View.GONE);
159+
}
160+
}
161+
}
162+
163+
@NonNull
164+
private List<MaterialButton> initializeFormatButtons(
165+
@NonNull List<FloatingToolbarLayout> floatingToolbars, @IdRes int formatButtonId) {
166+
List<MaterialButton> formatButtons = new ArrayList<>();
167+
for (FloatingToolbarLayout floatingToolbar : floatingToolbars) {
168+
formatButtons.add(floatingToolbar.findViewById(formatButtonId));
169+
}
170+
return formatButtons;
171+
}
172+
173+
private void propagateCheckedButtonState(List<MaterialButton> buttons, boolean isChecked) {
174+
for (MaterialButton button : buttons) {
175+
button.setChecked(isChecked);
176+
}
177+
}
178+
105179
@ColorInt
106180
private int getRandomColor() {
107181
Random random = new Random();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright (C) 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+
~ http://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+
<merge xmlns:android="http://schemas.android.com/apk/res/android"
17+
xmlns:app="http://schemas.android.com/apk/res-auto">
18+
<Button
19+
android:id="@+id/floating_toolbar_button_bold"
20+
style="?attr/materialIconButtonStyle"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:checkable="true"
24+
app:icon="@drawable/ic_format_bold_24px" />
25+
<Button
26+
android:id="@+id/floating_toolbar_button_italic"
27+
style="?attr/materialIconButtonStyle"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:checkable="true"
31+
app:icon="@drawable/ic_format_italic_24px" />
32+
<Button
33+
android:id="@+id/floating_toolbar_button_underlined"
34+
style="?attr/materialIconButtonStyle"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:checkable="true"
38+
app:icon="@drawable/ic_format_underlined_24px" />
39+
<Button
40+
android:id="@+id/floating_toolbar_button_color_text"
41+
style="?attr/materialIconButtonStyle"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
app:icon="@drawable/ic_format_color_text_24px" />
45+
<Button
46+
android:id="@+id/floating_toolbar_button_color_fill"
47+
style="?attr/materialIconButtonStyle"
48+
android:layout_width="wrap_content"
49+
android:layout_height="wrap_content"
50+
app:icon="@drawable/ic_format_color_fill_24px" />
51+
</merge>

0 commit comments

Comments
 (0)