Draw a Line in Android using Jetpack Compose Last Updated : 07 Mar, 2025 Comments Improve Suggest changes Like Article Like Report 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 could draw a straight line between two given points on the screen in Android using Jetpack Compose. Follow the below steps once the IDE is ready.Step by Step ImplementationStep 1 : Create a New Project in Android StudioTo create a new project in the Android Studio, please refer to How to Create a new Project in Android Studio with Jetpack Compose.Step 2 : Working with the MainActivity.kt fileGo 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.MainActivity.kt: Kotlin package com.geeksforgeeks.demo import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Canvas import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Surface import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import com.geeksforgeeks.demo.ui.theme.DemoTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { DemoTheme(dynamicColor = false, darkTheme = false) { Surface(color = Color.White) { DrawLine() } } } } } @Composable fun DrawLine() { Column( Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ) { // Creating a Canvas for drawing a straight // line between two points x and y Canvas(modifier = Modifier.fillMaxSize()) { // Fetching width and height for // setting start x and end y val canvasWidth = size.width val canvasHeight = size.height // drawing a line between start(x,y) and end(x,y) drawLine( start = Offset(x = canvasWidth, y = 0f), end = Offset(x = 0f, y = canvasHeight), color = Color.Red, strokeWidth = 5F ) } } } Output: Comment More infoAdvertise with us Next Article Draw a Line in Android using Jetpack Compose A aashaypawar Follow Improve Article Tags : Kotlin Android Android-Jetpack Similar Reads 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 ListView in Android using Jetpack Compose A ListView is a UI component that displays data in a vertically scrollable list, where each item is placed one below the other. It is widely used in Android applications to showcase categorized data in an organized manner. In Jetpack Compose, the traditional ListView from XML-based layouts is replac 2 min read Text in Android using Jetpack Compose Jetpack Compose is a new toolkit provided by Google. This is useful for designing beautiful UI designs. Android Text is a simple view in Android which is used to display text inside our App. In this article, we will take a look at the implementation of Text in Android using Jetpack Compose. Importan 5 min read How to Add Margin in Android using Jetpack Compose? In Android, Padding is used to offset the content of the view by a specific number of pixels from either direction, i.e., padding from left, right, top and bottom. Using Padding, we can create multiple borders to a view by applying a combination of multiple padding and border. Â So in this article, 2 min read Curved Text in Android using Jetpack Compose In Android, there are no inbuilt schemes to implement designed text like the word art, which is available in applications like MS Word. We can always refer to or download such schemes like designed font faces to implement designed text. However, it is also possible to natively build (building from s 3 min read Like