Implement Vertical and Horizontal Draggable Modifier in Android using Jetpack Compose
Last Updated :
20 Apr, 2022
In Jetpack Compose, a Draggable modifier is a modifier similar to scrollable, which is the high-level entry point to drag gestures in a single orientation and report the drag distance in pixels. Draggable modifier can be implemented on top of any element. Users can hold the element state and drag it vertically and horizontally.
In this article, we will show you how you could implement a Draggable Modifier 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.jcdraggablemodifier
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
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.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlin.math.roundToInt
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 | Draggable Modifier", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
// Creating Content
content = {
// Creating a Column Layout
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
// Horizontally Draggable Modifier
var offsetX by remember { mutableStateOf(0f) }
Text(
modifier = Modifier
.offset { IntOffset(offsetX.roundToInt(), 0) }
.draggable(
orientation = Orientation.Horizontal,
state = rememberDraggableState { delta ->
offsetX += delta
}
),
text = "I move Horizontally!", fontSize = 20.sp
)
// Adding a Space of 100dp height
Spacer(modifier = Modifier.height(100.dp))
// Vertically Draggable Modifier
var offsetY by remember { mutableStateOf(0f) }
Text(
modifier = Modifier
.offset { IntOffset(0, offsetY.roundToInt()) }
.draggable(
orientation = Orientation.Vertical,
state = rememberDraggableState { delta ->
offsetY += delta
}
),
text = "I move Vertically!", fontSize = 20.sp
)
}
}
)
}
}
}
Output:
In the below-recorded video, you can see that we are successfully able to drag elements vertically and horizontally.
Similar Reads
Create Vertical and Horizontal Divider in Android using Jetpack Compose As the name suggests, a Divider is something that divides the space into two. So basically, a divider can exist between two entities only. In Android, we can use a Divider to separate out two elements. It visually appears like a straight line segment. Elements can be basically stacked vertically or
2 min read
Horizontal ListView in Android using Jetpack Compose Many Android Applications present data in the form of horizontally scrollable lists. This list can be scrolled horizontally. In this article, we will take a look at How to Implement Horizontal ListView in Android Applications using Jetpack Compose. Note: If you are seeking Java code for Jetpack Com
6 min read
Android Jetpack Compose - Implement Navigation Drawer Jetpack Compose is a new UI toolkit from Google used to create native Android UI. It speeds up and simplifies UI development using less code, Kotlin APIs, and powerful tools.PrerequisitesFamiliar with Kotlin and OOP ConceptsBasic understanding of Jetpack ComposeThe navigation drawer is the most used
3 min read
Draw a Line in Android using Jetpack Compose In Android, a Canvas is used when we need to draw objects of different shapes and types. Canvas is a class in Android that performs 2D drawings of different objects onto the screen. It can also be used for drawing a straight line between two given points. So in this article, we will show you how you
2 min read
Date Picker in Android using Jetpack Compose In Android, a Date Picker is a widget used to select a date from the calendar. When a Date Picker is implemented in an application, the user can select a year, a month, and a day, combining to form date from a calendar-like-looking box. This data is generally needed and collected in applications tha
2 min read