26
26
import android .view .ViewGroup ;
27
27
import android .widget .TextView ;
28
28
import androidx .annotation .ColorInt ;
29
+ import androidx .annotation .IdRes ;
29
30
import androidx .annotation .NonNull ;
30
31
import androidx .annotation .Nullable ;
31
32
import com .google .android .material .button .MaterialButton ;
33
+ import com .google .android .material .floatingtoolbar .FloatingToolbarLayout ;
32
34
import io .material .catalog .feature .DemoFragment ;
35
+ import io .material .catalog .feature .DemoUtils ;
36
+ import java .util .ArrayList ;
37
+ import java .util .List ;
33
38
import java .util .Random ;
34
39
35
40
/** A fragment that displays the main Floating Toolbar demo for the Catalog app. */
@@ -46,62 +51,131 @@ public View onCreateDemoView(
46
51
layoutInflater .inflate (
47
52
R .layout .cat_floating_toolbar_fragment , viewGroup , /* attachToRoot= */ false );
48
53
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 ();
101
138
102
139
return view ;
103
140
}
104
141
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
+
105
179
@ ColorInt
106
180
private int getRandomColor () {
107
181
Random random = new Random ();
0 commit comments