1 /* 2 * 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 * 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 17 package androidx.compose.material3.windowsizeclass.samples 18 19 import android.os.Bundle 20 import androidx.activity.ComponentActivity 21 import androidx.activity.compose.setContent 22 import androidx.annotation.Sampled 23 import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi 24 import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass 25 import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass 26 import androidx.compose.runtime.Composable 27 28 @OptIn(ExperimentalMaterial3WindowSizeClassApi::class) 29 @Sampled AndroidWindowSizeClassSamplenull30fun AndroidWindowSizeClassSample() { 31 class MyActivity : ComponentActivity() { 32 override fun onCreate(savedInstanceState: Bundle?) { 33 super.onCreate(savedInstanceState) 34 setContent { 35 // Calculate the window size class for the activity's current window. If the window 36 // size changes, for example when the device is rotated, the value returned by 37 // calculateSizeClass will also change. 38 val windowSizeClass = calculateWindowSizeClass(this) 39 // Perform logic on the window size class to decide whether to use a nav rail. 40 val useNavRail = windowSizeClass.widthSizeClass > WindowWidthSizeClass.Compact 41 42 // MyScreen knows nothing about window size classes, and performs logic based on a 43 // Boolean flag. 44 MyScreen(useNavRail = useNavRail) 45 } 46 } 47 } 48 } 49 MyScreennull50@Suppress("UNUSED_PARAMETER") @Composable private fun MyScreen(useNavRail: Boolean) {} 51