How to enable Interactive Preview button for Jetpack Compose UI in Android Studio?

How to enable Interactive Preview button for Jetpack Compose UI in Android Studio?

Yitro Usman Author: Yitro Usman Date: 2022-10-05
How to enable Interactive Preview button for Jetpack Compose UI in Android Studio?

All you need to know about How to enable Interactive Preview button for Jetpack Compose UI in Android Studio? , in addintion to android - Choose for which device the preview is shown, jetpack compose , I cannot see interactive mode for the preview when using Jetpack Compose in Android Studio , android - Jetpack Compose LazyColumnFor deprecated, how to use LazyColumn with listState and list of objects? , android - How to position Floating Action Button to Left or Start in Jetpack Compose

  1. How to enable Interactive Preview button for Jetpack Compose UI in Android Studio?
  2. Question:

    No Interactive Preview Button

    I can't seem to find the Interactive Preview button on Android Studio, beside the deploy preview button. I am running Android Studio Arctic Fox | 2020.3.1 Beta 3.


    Solution 1:

    After looking around I got to know that Interactive Previews is an experimental feature, at least to the date of this answer. I had to enable it from Android Studio's: File -> Settings -> Experimental -> Enable interactive and animation preview tools. Here is the Link from the android developer website.

    After On

    After enabling it we can see the Interactive Preview button beside the deploy to device option.

  3. android - Choose for which device the preview is shown, jetpack compose
  4. Question:

    In the View system we could choose XML preview for different devices, but I could not find a way to choose preview device for Jetpack Compose. How do we set preview for different devices while using Jetpack Compose?


    Solution 1:

    You can do it like this

    @Preview(device = Devices.PIXEL_2_XL)
    @Composable
    fun DefaultPreview() {
        MyApplicationFooTheme {
            Greeting("Android")
        }
    }
    

    The list of available devices is here

    object Devices {
        const val DEFAULT = ""
    
        const val NEXUS_7 = "id:Nexus 7"
        const val NEXUS_7_2013 = "id:Nexus 7 2013"
        const val NEXUS_5 = "id:Nexus 5"
        const val NEXUS_6 = "id:Nexus 6"
        const val NEXUS_9 = "id:Nexus 9"
        const val NEXUS_10 = "name:Nexus 10"
        const val NEXUS_5X = "id:Nexus 5X"
        const val NEXUS_6P = "id:Nexus 6P"
        const val PIXEL_C = "id:pixel_c"
        const val PIXEL = "id:pixel"
        const val PIXEL_XL = "id:pixel_xl"
        const val PIXEL_2 = "id:pixel_2"
        const val PIXEL_2_XL = "id:pixel_2_xl"
        const val PIXEL_3 = "id:pixel_3"
        const val PIXEL_3_XL = "id:pixel_3_xl"
        const val PIXEL_3A = "id:pixel_3a"
        const val PIXEL_3A_XL = "id:pixel_3a_xl"
        const val PIXEL_4 = "id:pixel_4"
        const val PIXEL_4_XL = "id:pixel_4_xl"
    
        const val AUTOMOTIVE_1024p = "id:automotive_1024p_landscape"
    }
    

  5. I cannot see interactive mode for the preview when using Jetpack Compose in Android Studio
  6. Question:

    I am using Android Studio Arctic Fox | 2020.3.1 Beta 2 I cannot see the interactive mode icon in the preview. Is this version support such option?

    I want to be able to interact with the preview (for example click on the buttons) without deploying the apk on the device:

    enter image description here

    For now i see only the "Deploy preview" option. Is there something I am missing to use the interactive mode?


    Solution 1:

    This option is disabled by default on Arctic Fox because is considered experimental. So you must enable it in File > Settings > Experimental (Win) or Android Studio > Preferences > Experimental (Mac OS X)

    enter image description here

  7. android - Jetpack Compose LazyColumnFor deprecated, how to use LazyColumn with listState and list of objects?
  8. Question:

    As of Jetpack Compose 1.0.0-alpha09 LazyColumn, LazyColumnForIndexed, and row counterparts are deprecated. How is LazyColumn used, where, why, and how should i use rememberLazyListState?

    If you can provide a full example with items, state and onClick listener it would much obliged.


    Solution 1:

    This documentation here describes how to use LazyColumn instead of LazyColumnFor.

    https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/package-summary#lazycolumn

    Particular part of interest from the documentation:

    import androidx.compose.foundation.lazy.LazyColumn
    import androidx.compose.material.Text
    
    val itemsList = (0..5).toList()
    val itemsIndexedList = listOf("A", "B", "C")
    
    LazyColumn {
        items(itemsList) {
            Text("Item is $it")
        }
    
        item {
            Text("Single item")
        }
    
        itemsIndexed(itemsIndexedList) { index, item ->
            Text("Item at index $index is $item")
        }
    }
    

    Solution 2:

    With 1.0.0-beta06 the LazyColumn produces a vertically scrolling list.

    Something like:

    val itemsList = (0..30).toList()
    
    LazyColumn {
        items(itemsList) {
            Text("Item is $it")
        }
    }
    

    The LazyListState is a state object that can be hoisted to control and observe scrolling. It is created via rememberLazyListState.

    val listState = rememberLazyListState()
    

    It can be used to react and listen to scroll position and item layout changes.

    // Provide it to LazyColumn
    LazyColumn(state = liststate) {
        // Check if the first visible item is past the first item
       if (listState.firstVisibleItemIndex > 0){
           //...
       }
    }
    

    or to control the scroll position :

    // Remember a CoroutineScope to be able to launch
    val coroutineScope = rememberCoroutineScope()
    
    LazyColumn(state = listState) {
        // ...
    }
    
    lazyListState.animateScrollToItem(lazyListState.firstVisibleItemIndex)
    
    Button (
        onClick = { 
            coroutineScope.launch {
                // Animate scroll to item with index=5
                listState.animateScrollToItem(index = 5)
            }
        }
    ){
        Text("Click")
    }
    

  9. android - How to position Floating Action Button to Left or Start in Jetpack Compose
  10. Question:

    I want to create animated bottomAppBar in jetpack compose (something like image) but Fab position in jetpack compose is only center or end and i need to move FAB to left at least, my code is : enter image description here

    @Composable
    fun BottomBarSample() {
        Scaffold(
            floatingActionButton = {
                FloatingActionButton(
                    shape = CircleShape,
                    onClick = {},
                ) {
                    Icon(imageVector = Icons.Filled.Add, contentDescription = "icon")
                }
            },
            floatingActionButtonPosition = FabPosition.End,
            isFloatingActionButtonDocked = true,
            bottomBar = {
                BottomAppBar(backgroundColor = Color.Cyan, cutoutShape = CircleShape) {
    
                }
            }
        
        ){
            //body
        }
    }
    


    Solution 1:

    I guess this is not a good approach as we are changing the layout direction, but you can modify the layout direction from LTR to RTL using CompositionLocalProvider

    CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl ) {
           BottomBarSample()
    }
    

    enter image description here

    Solution 2:

    Since there is no FabPosition.Start yet using LocalLayoutDirection is easiest way to create cutout at the beginning. Other option is to create your layout using path operations or blending modes.

    In this example i show how to cut out using BlendModes but if you want elevation you need to use Path by creating shape as it's done in source code

    After changing layout direction to right to left you should change direction inside content, bottomBar or other lambdas you use

    @Composable
    fun BottomBarSample() {
        CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
            Scaffold(
                floatingActionButton = {
                    FloatingActionButton(
                        shape = CircleShape,
                        onClick = {},
                    ) {
                        Icon(imageVector = Icons.Filled.Add, contentDescription = "icon")
                    }
                },
                floatingActionButtonPosition = FabPosition.End,
                isFloatingActionButtonDocked = true,
                bottomBar = {
    
                    CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
                        BottomAppBar(backgroundColor = Color.Cyan, cutoutShape = CircleShape) {
    
                        }
                    }
    
    
                }
    
            ) {
                CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
                    //body
                }
            }
        }
    }