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 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.
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();
}
}
}
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