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 horizontally only. So a Divider can be of vertical or horizontal orientation. In this article, we will show you how you could implement a Vertical and Horizontal Divider 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.
package com.geeksforgeeks.jcdivider
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContent {
// Creating a Scaffold Layout
Scaffold(
// Creating a Top Bar
topBar = { TopAppBar(title = { Text( "GFG | Divider" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) },
content = {
// Creating a Column View
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
// Creating a Row to display two Text and a Divider between them
Row(Modifier.height(IntrinsicSize.Min).background(Color.Yellow)) {
Text( "Hello Geek!" , fontSize = 20 .sp)
Divider(color = Color.Green, modifier = Modifier.fillMaxHeight().width( 1 .dp))
Text( "This is a Divider" , fontSize = 20 .sp)
}
// Adding a Space of height 100dp
Spacer(modifier = Modifier.height( 100 .dp))
// Creating a Column to display two Text and a
// Divider between them
Column(
Modifier.width(IntrinsicSize.Max).background(Color.Cyan)) {
Text( "Hello Geek!" , fontSize = 20 .sp)
Divider(color = Color.Red, modifier = Modifier.fillMaxWidth().width( 1 .dp))
Text( "This is a Divider" , fontSize = 20 .sp)
}
}
}
)
}
}
} |
Output:
In the below screenshot of our application, you can see examples of vertical and horizontal dividers.