How to Generate QR Code in Android

feature-image

QR Codes are used in many softwares to show data in machine-readable form. These codes are used to display data in a secured way and that data is only readable by machines not by humans. We have seen many apps which provide QR code service, with the help of those apps we can scan QR code with our mobile device. In this post, we will learn how we can generate QR Code in Android. So for implementing this feature we will be using an existing library from Github.

Implementation of QR code Generator in Android:

We will be creating a simple Android application that will have an EditText field and a Button. The user will be adding some text in the EditText field and when the user clicks the Button then the application will convert that text (entered by the user) to QR code and we will display that QR code in an imageView. Below is the screenshot of the final app.

Note: We will be using Java language to Generate QR Code in Android.

Step by step implementation:
1. Create a new Android project

While creating a new android project select Java as a language as we are going to create our app using Java.

2. Adding dependency of the existing library

Go to the Gradle Scripts > build.gradle(Module: app) and add the below dependency in the dependencies section and click Sync now on the top right corner.

Note: While using this library you need to set your minSdk to 24 in the build.gradle file.

3. Setting up 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.

 

4. Working with the MainActivity.java file

Open your MainActivity.java file and copy the code below and paste it into your MainActivity.java file. I have added comments in the code to explain it.

 

Written by Hilal Ahmad