Open In App
Related Articles

How to Generate QR Code in Android?

Like Article
Save Article
Report issue

QR codes are used in many apps for displaying data in machine-readable form. These codes are used to represent data in a secured manner that is readable only by machines and not by humans. We have seen many apps that provide QR codes that and we can scan that 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 for implementing this feature we will be using a library from GitHub.

Implementation of QR Code Generator in Android

We will be creating a simple QR Code generator app in which we will be adding data from the app and generate a QR Code that will display the data inside it. Below is the sample GIF in which you will get a basic idea of what we are going to do in this article. Not that we are using Java language for building this project.

Generate QR Code in Android Sample GIF

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. Note that select Java 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.qrgenearator:QRGenearator:1.0.3’

Now sync the project from the top right corner option of Sync now. 

Step 3: Modify the strings.xml file

Below is the code for the strings.xml file.


    <string name="app_name">GFG App</string>
    <string name="qr_code">qr_code</string>
    <string name="enter_your_info">Enter your info</string>
    <string name="generate_qr_code">Generate QR Code</string>


Step 4: 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.


<?xml version="1.0" encoding="utf-8"?>
    <!--We are using this image 
        view to display our QR code-->
        android:contentDescription="@string/qr_code" />
    <!--Edit text to enter text 
        for creating a QR code-->
        android:inputType="text" />
    <!--Button for creating a QR code-->
        android:text="@string/generate_qr_code" />


Step 5: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidmads.library.qrgenearator.QRGContents;
import androidmads.library.qrgenearator.QRGEncoder;
public class MainActivity extends AppCompatActivity {
    // variables for imageview, edittext,
    // button, bitmap and qrencoder.
    private ImageView qrCodeIV;
    private EditText dataEdt;
    private Button generateQrBtn;
    Bitmap bitmap;
    QRGEncoder qrgEncoder;
    protected void onCreate(Bundle savedInstanceState) {
        // initializing all variables.
        qrCodeIV = findViewById(;
        dataEdt = findViewById(;
        generateQrBtn = findViewById(;
        // initializing onclick listener for button.
        generateQrBtn.setOnClickListener(new View.OnClickListener() {
            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 {
                    // below line is for getting 
                    // the windowmanager service.
                    WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
                    // initializing a variable for default display.
                    Display display = manager.getDefaultDisplay();
                    // creating a variable for point which 
                    // is to be displayed in QR Code.
                    Point point = new Point();
                    // getting width and 
                    // height of a point
                    int width = point.x;
                    int height = point.y;
                    // generating dimension from width and height.
                    int dimen = width < height ? width : height;
                    dimen = dimen * 3 / 4;
                    // setting this dimensions inside our qr code
                    // encoder to generate our qr code.
                    qrgEncoder = new QRGEncoder(dataEdt.getText().toString(), null, QRGContents.Type.TEXT, dimen);
                    try {
                        // getting our qrcode in the form of bitmap.
                        bitmap = qrgEncoder.encodeAsBitmap();
                        // the bitmap is set inside our image 
                        // view using .setimagebitmap method.
                    } catch (WriterException e) {
                        // this method is called for 
                        // exception handling.
                        Log.e("Tag", e.toString());



Note: You may also refer to How to Read QR Code in Android?

Check out the project:

Last Updated : 06 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads