Open In App

Draw Line with Pattern in Android using Jetpack Compose

Last Updated : 02 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

By Line Pattern, we mean various patterns that can be used to display a line. For example, dotted line (……), slashed line (/////////), hashed line (######) are different types of line patterns. Other than characters, there can be infinite ways of displaying a line pattern. Some are shown below:

Line with Pattern

 

In this article, we will show you how you could create a line pattern 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.linepattern
 
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.unit.dp
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 | Line Pattern", 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
                        Canvas(
                            Modifier
                                .padding(10.dp)
                                .height(30.dp)
                                .fillMaxWidth()
                                .clip(CircleShape)
                        ) {
                           
                            // Creating various parameters like step distance,
                            // tilt angle, count, size, etc and drawing a straight line
                            val step = 10.dp
                            val angleDegrees = 45f
                            val stepPx = step.toPx()
                            val stepsCount = (size.width / stepPx).roundToInt()
                            val actualStep = size.width / stepsCount
                            val dotSize = Size(width = actualStep / 2, height = size.height * 2)
                            for (i in -1..stepsCount) {
                                val rect = Rect(
                                    offset = Offset(x = i * actualStep, y = (size.height - dotSize.height) / 2),
                                    size = dotSize,
                                )
                                rotate(angleDegrees, pivot = rect.center) {
                                    drawRect(
                                        Color.Blue,
                                        topLeft = rect.topLeft,
                                        size = rect.size,
                                    )
                                }
                            }
                        }
                    }
                }
            )
        }
    }
}


Output:

You can see that we have created a line pattern as shown below.

Output

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads