1 /*
<lambda>null2 * Copyright 2022 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 @file:Suppress("DEPRECATION")
18
19 package com.google.accompanist.sample.themeadapter
20
21 import android.os.Bundle
22 import androidx.activity.ComponentActivity
23 import androidx.compose.animation.core.animateFloatAsState
24 import androidx.compose.foundation.layout.Box
25 import androidx.compose.foundation.layout.Column
26 import androidx.compose.foundation.layout.Row
27 import androidx.compose.foundation.layout.Spacer
28 import androidx.compose.foundation.layout.fillMaxSize
29 import androidx.compose.foundation.layout.height
30 import androidx.compose.foundation.layout.padding
31 import androidx.compose.foundation.layout.size
32 import androidx.compose.foundation.layout.width
33 import androidx.compose.foundation.rememberScrollState
34 import androidx.compose.foundation.selection.selectableGroup
35 import androidx.compose.foundation.verticalScroll
36 import androidx.compose.material.icons.Icons
37 import androidx.compose.material.icons.filled.Favorite
38 import androidx.compose.material3.Button
39 import androidx.compose.material3.Card
40 import androidx.compose.material3.CenterAlignedTopAppBar
41 import androidx.compose.material3.Checkbox
42 import androidx.compose.material3.CircularProgressIndicator
43 import androidx.compose.material3.ElevatedButton
44 import androidx.compose.material3.ExperimentalMaterial3Api
45 import androidx.compose.material3.ExtendedFloatingActionButton
46 import androidx.compose.material3.FilledTonalButton
47 import androidx.compose.material3.FloatingActionButton
48 import androidx.compose.material3.Icon
49 import androidx.compose.material3.LargeFloatingActionButton
50 import androidx.compose.material3.LinearProgressIndicator
51 import androidx.compose.material3.MaterialTheme
52 import androidx.compose.material3.OutlinedButton
53 import androidx.compose.material3.OutlinedTextField
54 import androidx.compose.material3.ProgressIndicatorDefaults
55 import androidx.compose.material3.RadioButton
56 import androidx.compose.material3.Scaffold
57 import androidx.compose.material3.Slider
58 import androidx.compose.material3.SmallFloatingActionButton
59 import androidx.compose.material3.Switch
60 import androidx.compose.material3.Text
61 import androidx.compose.material3.TextButton
62 import androidx.compose.material3.TextField
63 import androidx.compose.material3.TopAppBarDefaults
64 import androidx.compose.material3.rememberTopAppBarState
65 import androidx.compose.runtime.Composable
66 import androidx.compose.runtime.getValue
67 import androidx.compose.runtime.mutableStateOf
68 import androidx.compose.runtime.remember
69 import androidx.compose.runtime.saveable.rememberSaveable
70 import androidx.compose.runtime.setValue
71 import androidx.compose.ui.Alignment
72 import androidx.compose.ui.Modifier
73 import androidx.compose.ui.input.nestedscroll.nestedScroll
74 import androidx.compose.ui.platform.ComposeView
75 import androidx.compose.ui.res.stringResource
76 import androidx.compose.ui.tooling.preview.Preview
77 import androidx.compose.ui.unit.dp
78 import com.google.accompanist.sample.R
79 import com.google.accompanist.themeadapter.material3.Mdc3Theme
80 import com.google.android.material.color.DynamicColors
81
82 class Mdc3ThemeSample : ComponentActivity() {
83 override fun onCreate(savedInstanceState: Bundle?) {
84 super.onCreate(savedInstanceState)
85 DynamicColors.applyToActivityIfAvailable(this)
86 val contentView = ComposeView(this)
87 setContentView(contentView)
88 contentView.setContent {
89 Mdc3Theme {
90 Material3Sample()
91 }
92 }
93 }
94 }
95
96 @Preview
97 @Composable
Material3SamplePreviewnull98 fun Material3SamplePreview() {
99 Mdc3Theme {
100 Material3Sample()
101 }
102 }
103
104 @OptIn(ExperimentalMaterial3Api::class)
105 @Composable
Material3Samplenull106 fun Material3Sample() {
107 val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
108 Scaffold(
109 modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
110 topBar = {
111 CenterAlignedTopAppBar(
112 title = { Text(text = stringResource(R.string.themeadapter_title_material3)) },
113 scrollBehavior = scrollBehavior
114 )
115 }
116 ) { padding ->
117 Column(
118 modifier = Modifier
119 .verticalScroll(rememberScrollState())
120 .padding(padding)
121 .padding(16.dp)
122 ) {
123 Button(onClick = {}) {
124 Text(text = "Filled button")
125 }
126 VerticalSpacer()
127
128 ElevatedButton(onClick = {}) {
129 Text(text = "Elevated button")
130 }
131 VerticalSpacer()
132
133 FilledTonalButton(onClick = {}) {
134 Text(text = "Filled tonal button")
135 }
136 VerticalSpacer()
137
138 OutlinedButton(onClick = {}) {
139 Text(text = "Outlined button")
140 }
141 VerticalSpacer()
142
143 TextButton(onClick = {}) {
144 Text(text = "Text button")
145 }
146 VerticalSpacer()
147
148 SmallFloatingActionButton(
149 onClick = {},
150 content = { Icon(Icons.Default.Favorite, null) }
151 )
152 VerticalSpacer()
153
154 FloatingActionButton(
155 onClick = {},
156 content = { Icon(Icons.Default.Favorite, null) }
157 )
158 VerticalSpacer()
159
160 LargeFloatingActionButton(
161 onClick = {},
162 content = { Icon(Icons.Default.Favorite, null) }
163 )
164 VerticalSpacer()
165
166 ExtendedFloatingActionButton(
167 onClick = {},
168 text = { Text(text = "Extended FAB") },
169 icon = { Icon(Icons.Default.Favorite, null) }
170 )
171 VerticalSpacer()
172
173 Card(modifier = Modifier.size(width = 180.dp, height = 100.dp)) {
174 Box(
175 modifier = Modifier.fillMaxSize(),
176 contentAlignment = Alignment.Center
177 ) {
178 Text(text = "Card")
179 }
180 }
181 VerticalSpacer()
182
183 var checkboxChecked by remember { mutableStateOf(true) }
184 Checkbox(
185 checked = checkboxChecked,
186 onCheckedChange = { checkboxChecked = it }
187 )
188 VerticalSpacer()
189
190 var radioButtonChecked by remember { mutableStateOf(true) }
191 Row(Modifier.selectableGroup()) {
192 RadioButton(
193 selected = radioButtonChecked,
194 onClick = { radioButtonChecked = true }
195 )
196 RadioButton(
197 selected = !radioButtonChecked,
198 onClick = { radioButtonChecked = false }
199 )
200 }
201 VerticalSpacer()
202
203 var switchChecked by remember { mutableStateOf(true) }
204 Switch(
205 checked = switchChecked,
206 onCheckedChange = { switchChecked = it }
207 )
208 VerticalSpacer()
209
210 var linearProgress by remember { mutableStateOf(0.1f) }
211 val animatedLinearProgress by animateFloatAsState(
212 targetValue = linearProgress,
213 animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec
214 )
215 Row(verticalAlignment = Alignment.CenterVertically) {
216 LinearProgressIndicator(progress = animatedLinearProgress)
217 HorizontalSpacer()
218 TextButton(
219 onClick = {
220 if (linearProgress < 1f) linearProgress += 0.1f
221 }
222 ) {
223 Text("Increase")
224 }
225 }
226 VerticalSpacer()
227
228 var circularProgress by remember { mutableStateOf(0.1f) }
229 val animatedCircularProgress by animateFloatAsState(
230 targetValue = circularProgress,
231 animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec
232 )
233 Row(verticalAlignment = Alignment.CenterVertically) {
234 CircularProgressIndicator(progress = animatedCircularProgress)
235 HorizontalSpacer()
236 TextButton(
237 onClick = {
238 if (circularProgress < 1f) circularProgress += 0.1f
239 }
240 ) {
241 Text("Increase")
242 }
243 }
244 VerticalSpacer()
245
246 var sliderValue by remember { mutableStateOf(0f) }
247 Column {
248 Text(text = sliderValue.toString())
249 Slider(value = sliderValue, onValueChange = { sliderValue = it })
250 }
251 VerticalSpacer()
252
253 var text by rememberSaveable { mutableStateOf("") }
254 TextField(
255 value = text,
256 onValueChange = { text = it },
257 label = { Text("Text field") },
258 singleLine = true
259 )
260 VerticalSpacer()
261
262 var outlinedText by rememberSaveable { mutableStateOf("") }
263 OutlinedTextField(
264 value = outlinedText,
265 onValueChange = { outlinedText = it },
266 label = { Text("Outlined text field") },
267 singleLine = true
268 )
269 VerticalSpacer()
270
271 Text(
272 text = "Display Large",
273 style = MaterialTheme.typography.displayLarge
274 )
275 Text(
276 text = "Display Medium",
277 style = MaterialTheme.typography.displayMedium
278 )
279 Text(
280 text = "Display Small",
281 style = MaterialTheme.typography.displaySmall
282 )
283 Text(
284 text = "Headline Large",
285 style = MaterialTheme.typography.headlineLarge
286 )
287 Text(
288 text = "Headline Medium",
289 style = MaterialTheme.typography.headlineMedium
290 )
291 Text(
292 text = "Headline Small",
293 style = MaterialTheme.typography.headlineSmall
294 )
295 Text(
296 text = "Title Large",
297 style = MaterialTheme.typography.titleLarge
298 )
299 Text(
300 text = "Title Medium",
301 style = MaterialTheme.typography.titleMedium
302 )
303 Text(
304 text = "Title Small",
305 style = MaterialTheme.typography.titleSmall
306 )
307 Text(
308 text = "Body Large",
309 style = MaterialTheme.typography.bodyLarge
310 )
311 Text(
312 text = "Body Medium",
313 style = MaterialTheme.typography.bodyMedium
314 )
315 Text(
316 text = "Body Small",
317 style = MaterialTheme.typography.bodySmall
318 )
319 Text(
320 text = "Label Large",
321 style = MaterialTheme.typography.labelLarge
322 )
323 Text(
324 text = "Label Medium",
325 style = MaterialTheme.typography.labelMedium
326 )
327 Text(
328 text = "Label Small",
329 style = MaterialTheme.typography.labelSmall
330 )
331 }
332 }
333 }
334
335 @Composable
VerticalSpacernull336 private fun VerticalSpacer() {
337 Spacer(Modifier.height(8.dp))
338 }
339
340 @Composable
HorizontalSpacernull341 private fun HorizontalSpacer() {
342 Spacer(Modifier.width(8.dp))
343 }
344