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  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 ''

 compile ''


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 file.

package com.android_examples.generateqrcode_android_examplescom;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

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(;
        editText = (EditText)findViewById(;
        button = (Button)findViewById(;

        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=""

        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.

    • I also need the same. One app to generate dynamic QR Code and another to Scan it. After pairing the device I will perform some functions.

      Please help with the same

  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:

  3. Please give a explain of Code

  4. Abhinav Kumar Pandey

    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.

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

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

    • Kesh just download and import this project into studio.

    • I have resolved these errors.
      Under app/res/values/colors.xml add below mentioned two lines:

      Now check your file these errors will not be shown now.

  7. I would like to make the qr image bigger, how do i do that??

  8. can you please provide sample of Scaning QR Code in Android Studio?.

  9. Hi, I would like to know if is there any way for the QR code to support Chinese charcters. For example, if i were to type 你好 in the editText, it still generates the QR code but when i scan it, it gives me ???.

  10. It is giving 2 errors saying that QRCodeBlackColor and QRCodeWhiteColor are symbols which cannot be resolved. Please HELP me to resolve this i want to add this qr code into another very important project.PLEASE help me out.

Leave a Reply

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