How to add a Vertical Line in Android Jetpack Compose

By Mohammed Rashid •  November 13th, 2022 • 

Lines are helpful to divide different UI elements of mobile apps. In this simple and short Jetpack Compose tutorial let’s learn how to add a vertical line.

You can use Divider from Jetpack Compose to draw lines- horizontal or vertical. Wrapping a Divider with Row makes the line vertical. See the code snippet given below.

@Composable
fun Example() {
    Row(
        horizontalArrangement = Arrangement.Center
    ) {
        Divider(modifier = Modifier.fillMaxHeight().width(1.dp), color = Color.Black)
    }
}

Following is the output.

Following is the complete code of this Jetpack Compose example to show vertical line.

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Example()
                }
            }
        }
    }
}

@Composable
fun Example() {
    Row(
        horizontalArrangement = Arrangement.Center
    ) {
        Divider(modifier = Modifier.fillMaxHeight().width(1.dp), color = Color.Black)
    }
}


@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApplicationTheme {
        Example()
    }
}

I hope this blog post will be helpful for you.

Mohammed Rashid

Keep Reading