Open In App

Draw a Line in Android using Jetpack Compose

Last Updated : 26 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
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 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.jcdrawline
  
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.material.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
  
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 | Draw a Straight Line", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
  
                // Creating Content
                content = {
  
                    // Creating a Column Layout
                    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:

You can see that we are able to draw a straight line between two diagonal points.

Output

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads