Generate QR Code in Android using Kotlin
Many applications nowadays use QR codes within their application to hide some information. QR codes are seen within many payment applications which are used by the users to scan and make payments. The QR codes which are seen within these applications are generated inside the application itself. In this article, we will take a look at How to generate a QR code in Android using Kotlin. A sample video is given below to get an idea about what we are going to do in this article.
Note: If you are looking to generate QR code in android using Java. Check out the following article: How to Generate QR Code in Android using Java
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. Note that select Kotlin as the programming language.
Step 2: Add dependency to build.gradle(Module:app)
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation 'androidmads.library.qrgenerator:QRGenerator:1.0.3'
After adding this dependency simply sync this project to install it.
Step 3: 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. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/container" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--on below line we are creating a text for heading of our app--> < TextView android:id = "@+id/idTVHeading" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:gravity = "center" android:padding = "4dp" android:text = "QR Code Generator" android:textAlignment = "center" android:textColor = "@color/purple_200" android:textSize = "18sp" android:textStyle = "bold" /> <!--We are using this image view to display our QR code--> < ImageView android:id = "@+id/idIVQrcode" android:layout_width = "300dp" android:layout_height = "300dp" android:layout_centerHorizontal = "true" android:layout_marginTop = "50dp" android:contentDescription = "QR Code" /> <!--Edit text to enter text for creating a QR code--> < EditText android:id = "@+id/idEdt" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@id/idIVQrcode" android:layout_marginStart = "20dp" android:layout_marginTop = "30dp" android:layout_marginEnd = "20dp" android:autofillHints = "" android:hint = "Enter your message" android:inputType = "text" /> <!--Button for creating a QR code--> < Button android:id = "@+id/idBtnGenerateQR" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@id/idEdt" android:layout_marginStart = "20dp" android:layout_marginTop = "30dp" android:layout_marginEnd = "20dp" android:text = "Generate QR Code" android:textAllCaps = "false" /> </ RelativeLayout > |
Step 4: 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.gtappdevelopers.kotlingfgproject import android.graphics.Bitmap import android.graphics.Point import android.os.Bundle import android.text.TextUtils import android.view.Display import android.view.WindowManager import android.widget.Button import android.widget.EditText import android.widget.ImageView import android.widget.Toast import androidmads.library.qrgenerator.QRGContents import androidmads.library.qrgenerator.QRGEncoder import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // on below line we are creating a variable // for our image view, edit text and a button. lateinit var qrIV: ImageView lateinit var msgEdt: EditText lateinit var generateQRBtn: Button // on below line we are creating // a variable for bitmap lateinit var bitmap: Bitmap // on below line we are creating // a variable for qr encoder. lateinit var qrEncoder: QRGEncoder override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are // initializing our all variables. qrIV = findViewById(R.id.idIVQrcode) msgEdt = findViewById(R.id.idEdt) generateQRBtn = findViewById(R.id.idBtnGenerateQR) // on below line we are adding on click // listener for our generate QR button. generateQRBtn.setOnClickListener { // on below line we are checking if msg edit text is empty or not. if (TextUtils.isEmpty(msgEdt.text.toString())) { // on below line we are displaying toast message to display enter some text Toast.makeText(applicationContext, "Enter your message" , Toast.LENGTH_SHORT).show() } else { // on below line we are getting service for window manager val windowManager: WindowManager = getSystemService(WINDOW_SERVICE) as WindowManager // on below line we are initializing a // variable for our default display val display: Display = windowManager.defaultDisplay // on below line we are creating a variable // for point which is use to display in qr code val point: Point = Point() display.getSize(point) // on below line we are getting // height and width of our point val width = point.x val height = point.y // on below line we are generating // dimensions for width and height var dimen = if (width < height) width else height dimen = dimen * 3 / 4 // on below line we are initializing our qr encoder qrEncoder = QRGEncoder(msgEdt.text.toString(), null , QRGContents.Type.TEXT, dimen) // on below line we are running a try // and catch block for initializing our bitmap try { // on below line we are // initializing our bitmap bitmap = qrEncoder.encodeAsBitmap() // on below line we are setting // this bitmap to our image view qrIV.setImageBitmap(bitmap) } catch (e: Exception) { // on below line we // are handling exception e.printStackTrace() } } } } } |
Now run your application to see the output of it.
Output: