Kotlin Generate QRCode Using Zxing Android Studio Programmatically

Kotlin Generate QR Code tutorial with example is what you are learning now.

In this tutorial, we have used ZXing library to generate qr code from string.

We have also explained how you can save generated qr code in android device.

If you are making app related to grocery products or so you need to maintain unique qr code for each product.

This is where this tutorial will help you a lot.

User will enter any string which he likes and app will generate qr code for that particular string.

If you want to generate qr code using Java then follow android qr code generator example. 

Download source code for kotlin generate qr code example

Step 1. ZXing Dependencies

ZXing dependencies are required to be added to use their classes.

Add below line in the build.grdale(Module:app) file

Full code will look like

Step 3. Add black and white colors

As you already know that every qr code image includes only two colors – Black and White

So let’s add these colors in the colors.xml file

Go to res->values->colors.xml directory and add following

Whole code for colors.xml

Step 4. Permissions in manifest

This tutorial required read and write permissions for internal storage of android device.

Put below lines in AndroidManifest.xml

So final code for AndroidManifest.xml is like below

Step 5. Updating Main Activity

Finally, change main activity codes and our example is ready.

replace your whole code for activity_main.xml with below one

Now it is time to add code to generate qr code image.

Add some kotlin coding into the MainActivity.kt file

Understanding above code

Following method is responsible for QR Code generation from given string.

Above method uses various classes from ZXing library.

Now look at the below method, which is saving generated qr code image into the device.

After implementing all the above steps, you should get following output

If you are not able to get result like above video, share your questions in comment section.

Keep rocking and have a nice day ahead!

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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