Generate QR code in android using Zxing library in Android Studio example tutorial

How to make QR code in android using EditText entered string or integer value at application run time on button click.

QR code also known as Quick Response Code obtain an array of white and black color small blocks mostly used to store or convert any kind of URL , string name and integer value into blocks mode. QR code is now very much popular in android development because with the use of it android developers can easily determine any android device and connect them with each other without typing their username or password. So here is the complete step by step tutorial for Generate QR code in android using Zxing library in Android Studio example tutorial.

What we are doing in this project : We are creating an android application with the use of Zxing library and inside the app user can just simply enter any kind of value ( URL, NAME, NUMBER ) and click on button, After click it will automatically convert the entered value to QR code.


Follow all the below steps very carefully to add Zxing library in your project.

1. Open your Project’s build.gradle ( Module : app ) and  add com.android.support:appcompat-v7:23.4.0  library file.


2. Here is the code to add appcompat library inside build.gradle file.

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:appcompat-v7:24.0.0'

 compile 'com.google.zxing:core:3.2.1'


Screenshot after add this code into build.gradle file.


Here you go now our library is successfully imported next step is to start coding.

How to Generate QR code in android using Zxing library in Android Studio example tutorial.

Code for MainActivity.java file.

package com.android_examples.generateqrcode_android_examplescom;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    Button button;
    EditText editText;
    String EditTextValue ;
    Thread thread ;
    public final static int QRcodeWidth = 500 ;
    Bitmap bitmap ;
    protected void onCreate(Bundle savedInstanceState) {

        imageView = (ImageView)findViewById(R.id.imageView);
        editText = (EditText)findViewById(R.id.editText);
        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                EditTextValue = editText.getText().toString();

                try {
                    bitmap = TextToImageEncode(EditTextValue);


                } catch (WriterException e) {


    Bitmap TextToImageEncode(String Value) throws WriterException {
        BitMatrix bitMatrix;
        try {
            bitMatrix = new MultiFormatWriter().encode(
                    QRcodeWidth, QRcodeWidth, null

        } catch (IllegalArgumentException Illegalargumentexception) {

            return null;
        int bitMatrixWidth = bitMatrix.getWidth();

        int bitMatrixHeight = bitMatrix.getHeight();

        int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

        for (int y = 0; y < bitMatrixHeight; y++) {
            int offset = y * bitMatrixWidth;

            for (int x = 0; x < bitMatrixWidth; x++) {

                pixels[offset + x] = bitMatrix.get(x, y) ?
        Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

        bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);
        return bitmap;

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:hint="Enter Text Here"

        android:text="CLICK HERE TO generate qr code"
        android:layout_centerHorizontal="true" />






Generate QR code in android using Zxing library in Android Studio example tutorial

Click here to download Generate QR code in android using Zxing library in Android Studio example tutorial project with source code.


  1. Hi,
    I have seen your two programs.
    1. Generate QR code
    2. Scan QR code.
    Now, i want to install two application in two different devices. In one device, QR code will be generated and the other will scan the generated QR code. After this i want to perform some function. But this functions can be done only after pairing the devices via QR code only.
    Is it possible to do. If yes, then please help me with the source code.

  2. The getColor is already deprecated, you may change this line:
    pixels[offset + x] = bitMatrix.get(x, y) ? getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);

    to this:
    pixels[offset + x] = bitMatrix.get(x, y) ? getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);

    and dont forget to import contextcompat like below:
    import android.support.v4.content.ContextCompat;

      • How can you configure the error correction level in this code?
        Its currently set to null which defaults to Low level error correction (7%). Doesn’t allow a user to change it easily.
        There are supposed to be 4 levels – L,M,Q & H. – L = Low (default) M and Q intermediate and H = High 30% (I think…)
        However, these are supposed to be passed as EnumMap.
        In this code you can’t directly pass something like “H” or even EncodeHintType.H etc. Need to define the map first. Adding a drop down next to the text entry to choose the coding level would be great too! It will complete the offering…

  3. i want to create 8 fields ,where to do the changes to create the qr code from 8 fields.

    • Abhinav there is no need to create 8 fields because it will automatically convert every inserted text into QR code.

  4. i want to have 8 fields like name , address, mobile number ,email and much more . and for that i have created 8 edit text , so how can i implement the code .

    • Abhinav you can murge all name , address, mobile number ,email into a single string and separate them using comma or plus sign then generate QR code for whole combine string.

    • EditTextValue = editText.getText().toString();
      String XML =”{‘name’:'”+EditTextValue+”‘,’address’:’South Gate’} ” ;

      try {
      bitmap = TextToImageEncode(XML);


      } catch (WriterException e) {

      • Then you cal call the above using a JSON
        try {
        //converting the data to json
        JSONObject obj = new JSONObject(result.getContents());
        //setting values to textviews
        } catch (JSONException e) {
        //if control comes here
        //that means the encoded format not matches
        //in this case you can display whatever data is available on the qrcode
        //to a toast
        Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();

  5. It is giving 2 errors saying that QRCodeBlackColor and QRCodeWhiteColor are symbols which cannot be resolved. Any help?

Leave a Reply

Your email address will not be published. Required fields are marked *