Open In App

Scrollable Modifier Monitored By an Offset Value in Android using Jetpack Compose

Last Updated : 26 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, a Scrollable modifier detects the scroll gestures, and calculates the scroll offset value, but does not offset its contents. In simpler words, the Scroll Modifier will calculate the amount of scroll performed in a particular orientation (vertical or horizontal), but will actually not displace the view or the element in either scroll direction. We can assume the example of a computer mouse, where the mouse moves over the screen as well as on the trackpad but the trackpad does not change its position. Common applications can be volume bar and screen brightness bar.

Scrollable Modifier Monitored by An Offset Value in Android using Jetpack Compose

 

In this article, we will show you how you could create a Scrollable Modifier and monitor it in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcscrollablemodifier
  
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.rememberScrollableState
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
  
            // Creating a Simple Scaffold 
              // Layout for the application
            Scaffold(
  
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Scrollable Modifier", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
  
                // Creating Content
                content = {
  
                    // Creating a Column Layout
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                        // actual composable state
                        var offset by remember { mutableStateOf(0f) }
                          
                        Box(
                            Modifier
                                .size(150.dp)
                                .scrollable(
                                    orientation = Orientation.Vertical,
                                    // Scrollable state: describes how to consume
                                    // scrolling delta and update offset
                                    state = rememberScrollableState { delta ->
                                        offset += delta
                                        delta
                                    }
                                )
                                .background(Color.LightGray),
                            contentAlignment = Alignment.Center
                        ) {
                            Text(offset.toString())
                        }
                    }
                }
            )
        }
    }
}


Output:

You can see that we are able to create a Scrollable Modifier and monitor its offset value.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads