How to Generate QR Code in Android?
Last Updated :
22 Apr, 2025
QR codes are used in many apps to display data in machine-readable form. These codes are used to represent data in a secure manner that is readable only by machines and not by humans. We have seen many apps that provide QR codes and we can scan those QR codes with our mobile device. In this article, we will take a look at how we can generate a QR Code for our app. So to implement this feature, we will be using a library from GitHub.
Note: You may also refer to How to Read QR Code in Android?
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Add dependency to build.gradle.kts (Module:app)
- Navigate to the Gradle Scripts > build.gradle.kts (Module:app) and add the below dependency in the dependencies section.
dependencies {
...
implementation ("com.journeyapps:zxing-android-embedded:4.3.0")
}
Refer to the following link for the documentation: Click Here
- Navigate to the Gradle Scripts > settings.gradle.kts and add the below line of code in the repositories section.
dependencyResolutionManagement {
...
repositories {
...
mavenCentral()
}
}
Now sync the project from the top right corner option of Sync now.
Step 3: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<!--We are using this image
view to display our QR code-->
<ImageView
android:id="@+id/idIVQrcode"
android:layout_width="300dp"
android:layout_height="300dp" />
<!--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_marginStart="20dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="20dp"
android:hint="Enter your info"
android:inputType="text" />
<!--Button for creating a QR code-->
<Button
android:id="@+id/idBtnGenerateQR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="20dp"
android:text="Generate QR Code" />
</LinearLayout>
Step 4: Working with the MainActivity file
Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
MainActivity.java
package org.geeksforgeeks.demo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.journeyapps.barcodescanner.BarcodeEncoder;
public class MainActivity extends AppCompatActivity {
// Variables for imageview, edittext,
// button, bitmap and qrencoder.
private ImageView qrCodeIV;
private EditText dataEdt;
private Button generateQrBtn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing all variables.
qrCodeIV = findViewById(R.id.idIVQrcode);
dataEdt = findViewById(R.id.idEdt);
generateQrBtn = findViewById(R.id.idBtnGenerateQR);
// Initializing onclick listener for button.
generateQrBtn.setOnClickListener(
new View.OnClickListener() {
@Override public void onClick(View v)
{
if (TextUtils.isEmpty(
dataEdt.getText().toString())) {
// If the edittext inputs are empty
// then execute this method showing
// a toast message.
Toast.makeText(MainActivity.this,"Enter some text to generate QR Code",Toast.LENGTH_SHORT).show();
}
else {
generateQRCode(
dataEdt.getText().toString());
}
}
});
}
private void generateQRCode(String text)
{
BarcodeEncoder barcodeEncoder
= new BarcodeEncoder();
try {
// This method returns a Bitmap image of the
// encoded text with a height and width of 400
// pixels.
Bitmap bitmap = barcodeEncoder.encodeBitmap(text, BarcodeFormat.QR_CODE, 400, 400);
// Sets the Bitmap to ImageView
qrCodeIV.setImageBitmap(bitmap);
}
catch (WriterException e) {
e.printStackTrace();
}
}
}
MainActivity.kt
package org.geeksforgeeks.demo
import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.zxing.BarcodeFormat
import com.google.zxing.WriterException
import com.journeyapps.barcodescanner.BarcodeEncoder
class MainActivity : AppCompatActivity() {
// Variables for imageview, edittext,
// button, bitmap and encoder.
private lateinit var qrCodeIV: ImageView
private lateinit var dataEdt: EditText
private lateinit var generateQrBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initializing all variables.
qrCodeIV = findViewById(R.id.idIVQrcode)
dataEdt = findViewById(R.id.idEdt)
generateQrBtn = findViewById(R.id.idBtnGenerateQR)
// Initializing onclick listener for button.
generateQrBtn.setOnClickListener {
if (TextUtils.isEmpty(
dataEdt.getText().toString()
)
) {
// If the edittext inputs are empty
// then execute this method showing
// a toast message.
Toast.makeText(
this@MainActivity,
"Enter some text to generate QR Code",
Toast.LENGTH_SHORT
).show()
}
else {
generateQRCode(
dataEdt.getText().toString()
)
}
}
}
private fun generateQRCode(text: String) {
val barcodeEncoder = BarcodeEncoder()
try {
// This method returns a Bitmap image of the
// encoded text with a height and width of 400
// pixels.
val bitmap: Bitmap = barcodeEncoder.encodeBitmap(text, BarcodeFormat.QR_CODE, 400, 400)
qrCodeIV.setImageBitmap(bitmap) // Sets the Bitmap to ImageView
} catch (e: WriterException) {
e.printStackTrace()
}
}
}
Refer to the following github repo to get the entire code: Generate-QR-Code-in-Android
Output:
Similar Reads
How to Generate Barcode in Android? Barcodes are the graphical representation of data that enables effortless scanning. When we aim to incorporate this functionality for various purposes in Android applications it becomes crucial to understand the underlying principles and techniques involved in generating barcodes effectively. In thi
3 min read
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 t
4 min read
How to Generate a PDF file in Android App? There are many apps in which data from the app is provided to users in the downloadable PDF file format. So in this case we have to create a PDF file from the data present inside our app and represent that data properly inside our app. So by using this technique, we can easily create a new PDF accor
6 min read
How to Generate Pattern Password in Android? Pattern Password for the device is one of the necessities to keep our device private and protected. Nowadays in most apps, we get to see this password applied in many applications such as Mutual Funds or Stock Market apps to keep our financial details private. In this article, we are going to see ho
6 min read
How to Create a Paint Application in Android? We all have once used the MS-Paint in our childhood, and when the system was shifted from desks to our palms, we started doodling on Instagram Stories, Hike, WhatsApp, and many more such apps. But have you ever thought about how these functionalities were brought to life? So, In this article, we wil
8 min read
How to Create a Dice Roller App in Android? A dice roller application is a simple application that generates a random number between 1 and a specified maximum number, simulating the roll of a dice. The application is typically used by gamers or anyone who needs to roll a die but doesn't have physical dice available. To create the app, you nee
2 min read
How to Create an ImageButton in Android? Nowadays, image buttons play a big role in making the android application more interactive and user-friendly. Be it any social media app like Instagram or Facebook or any shopping app or streaming app, each application uses this feature widely. In this article, we will take a look at the implementat
3 min read
How to Convert a Vector to Bitmap in Android? A vector is a set of points, lines, and colors associated with any image object defined inside an XML file. All these associated attributes are compiled in real-time to develop an image object. Simply, a vector is a coded representation of an image object. Bitmap, also known as bitmap index or bit a
3 min read
Generate PDF File in Android using Kotlin Most of the applications provide users with the facility of bills to be downloaded from the mobile applications. This type of application gets the data from the APIS or data within the application and this data is used to generate the PDF files. PDF files within android are generated using canvas. I
7 min read
How to Encrypt and Decrypt Images in Android? Many times when we are building an android application we have to encrypt the data entered by the user within the android application to maintain the data privacy and security of the application. Along with that we also have to decrypt the data to access it. In this article, we will take a look at H
7 min read