18
18
19
19
package com.example.compose.snippets.layouts
20
20
21
- import android.os.Bundle
22
- import androidx.activity.ComponentActivity
23
- import androidx.activity.compose.setContent
24
21
import androidx.compose.foundation.layout.BoxWithConstraints
25
22
import androidx.compose.foundation.layout.Column
26
23
import androidx.compose.foundation.layout.Row
27
- import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
28
- import androidx.compose.material3.windowsizeclass.WindowHeightSizeClass
29
- import androidx.compose.material3.windowsizeclass.WindowSizeClass
30
- import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
24
+ import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
25
+ import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
31
26
import androidx.compose.runtime.Composable
32
27
import androidx.compose.runtime.getValue
33
28
import androidx.compose.runtime.mutableStateOf
34
29
import androidx.compose.runtime.remember
35
30
import androidx.compose.runtime.setValue
36
31
import androidx.compose.ui.unit.dp
32
+ import androidx.window.core.layout.WindowHeightSizeClass
33
+ import androidx.window.core.layout.WindowSizeClass
37
34
38
35
/*
39
36
* Copyright 2023 The Android Open Source Project
@@ -51,25 +48,15 @@ import androidx.compose.ui.unit.dp
51
48
* limitations under the License.
52
49
*/
53
50
// [START android_compose_adaptive_layouts_basic]
54
- class MainActivity : ComponentActivity () {
55
- @OptIn(ExperimentalMaterial3WindowSizeClassApi ::class )
56
- override fun onCreate (savedInstanceState : Bundle ? ) {
57
- super .onCreate(savedInstanceState)
58
-
59
- setContent {
60
- val windowSizeClass = calculateWindowSizeClass(this )
61
- MyApp (windowSizeClass)
62
- }
63
- }
64
- }
51
+ @OptIn(ExperimentalMaterial3AdaptiveApi ::class )
65
52
@Composable
66
- fun MyApp (windowSizeClass : WindowSizeClass ) {
67
- // Perform logic on the size class to decide whether to show
68
- // the top app bar.
69
- val showTopAppBar = windowSizeClass.heightSizeClass != WindowHeightSizeClass .Compact
53
+ fun MyApp (
54
+ windowSizeClass : WindowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
55
+ ) {
56
+ // Perform logic on the size class to decide whether to show the top app bar.
57
+ val showTopAppBar = windowSizeClass.windowHeightSizeClass != WindowHeightSizeClass .COMPACT
70
58
71
- // MyScreen knows nothing about window sizes, and performs logic
72
- // based on a Boolean flag.
59
+ // MyScreen knows nothing about window sizes, and performs logic based on a Boolean flag.
73
60
MyScreen (
74
61
showTopAppBar = showTopAppBar,
75
62
/* ... */
@@ -95,6 +82,13 @@ fun AdaptivePane(
95
82
}
96
83
// [END android_compose_layouts_adaptive_pane]
97
84
85
+ @Composable
86
+ private fun WindowSizeClassSnippet () {
87
+ // [START android_compose_windowsizeclass]
88
+ val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
89
+ // [END android_compose_windowsizeclass]
90
+ }
91
+
98
92
@Composable
99
93
fun OnePane () {
100
94
// your content here
0 commit comments