Draw Straight Line Between Two Points in Android
Last Updated :
06 Feb, 2022
In Android, ImageView is used to display images of different types of formats. However, ImageView can also be used to draw desired patterns using a canvas and paint. So in this article, we will show you how you could draw a line programmatically and display it in an ImageView in Android. Follow the below steps once the IDE is ready. So basically, we would be creating a line depending on the coordinate points. This process is mentioned in Step 3 where we work with MainActivity.kt.
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. 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 activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add an ImageView and a Button as shown below.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="draw"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50sp"/>
</RelativeLayout>
Step 3: 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 org.geeksforgeeks.drawlineiniv
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring and initializing
// the elements from the layout file
val mImageView = findViewById<ImageView>(R.id.image_view_1)
val mButton = findViewById<Button>(R.id.button_1)
// Create a Bitmap of 500 x 700 size
val mBitmap = Bitmap.createBitmap(500, 700, Bitmap.Config.ARGB_8888)
// Create a canvas with gray background
// and set it in ImageView
val mCanvas = Canvas(mBitmap)
mCanvas.drawColor(Color.GRAY)
mImageView.setImageBitmap(mBitmap)
// On Button click
mButton.setOnClickListener {
// Paint is called, assume it as a paint bristle
// with green paint and 10 as thickness
val mPaint = Paint()
mPaint.color = Color.GREEN
mPaint.style = Paint.Style.STROKE
mPaint.strokeWidth = 10F
mPaint.isAntiAlias = true
// Declaring start and end
// coordinates on the canvas
val mStartX = 0F
val mStartY = 0F
val mStopX = mCanvas.width.toFloat()
val mStopY = mCanvas.height.toFloat()
// Draw the line
mCanvas.drawLine(mStartX, mStartY, mStopX, mStopY, mPaint)
// Set this bitmap in the ImageView
mImageView.setImageBitmap(mBitmap)
}
}
}
Output:
You can see that the line is created between the mentioned coordinates.
Similar Reads
Find Days Between Two Dates in Android There are many travels and accommodation-based Android applications which we use to check and buy tickets for any traveling and hotel booking. Inputs such as origin, traveling destination, selection of hotel, and trip dates are needed to book appropriate options. In the case of hotel booking, the nu
3 min read
Android - Point Graph Series with Kotlin Graphs are used to represent vast amounts of data in an easily readable form. There are several graphs used to display data such as simple bar graphs, group bar graphs, point graph series, and others. In this article, we will take a look at the implementation of Point Graph Series in Android applica
3 min read
Draw a Line in Android using Jetpack Compose 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
2 min read
Android Jetpack Compose - Calculate Distance Between Two Locations We have seen many apps that use Google Maps. These maps are used to indicate places on Google Maps and also we can use this to calculate the distance between two locations. In this article, we will take a look at calculating the distance between two locations in Android. A sample video is given belo
7 min read
How to Draw Different Types of Circles in Android? In Android, we can manually create shapes as required. A shape can be designed in XML by creating an Android Resource File. The type of file allows various attributes like dimensions, color, strokes (border), solid (background), etc. for creating a desired shape and design. Basically, in this articl
3 min read