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.
1 |
implementation 'com.journeyapps:zxing-android-embedded:4.1.0' |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- text to be converted to QR code --> <EditText android:id="@+id/etText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="38dp" android:layout_marginEnd="32dp" android:ems="10" android:hint="Enter text here" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <!-- button to generate QR code --> <Button android:id="@+id/btnGenerate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="Generate code" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/etText" /> <!-- imageview to display the generated QR code --> <ImageView android:id="@+id/imageCode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="100dp" android:layout_marginEnd="32dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnGenerate"/> </androidx.constraintlayout.widget.ConstraintLayout> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Button for generating QR code Button btnGenerate = findViewById(R.id.btnGenerate); //Text will be entered here to generate QR code EditText etText = findViewById(R.id.etText); //ImageView for generated QR code ImageView imageCode = findViewById(R.id.imageCode); btnGenerate.setOnClickListener(v -> { //getting text from input text field. String myText = etText.getText().toString().trim(); //initializing MultiFormatWriter for QR code MultiFormatWriter mWriter = new MultiFormatWriter(); try { //BitMatrix class to encode entered text and set Width & Height BitMatrix mMatrix = mWriter.encode(myText, BarcodeFormat.QR_CODE, 400,400); BarcodeEncoder mEncoder = new BarcodeEncoder(); Bitmap mBitmap = mEncoder.createBitmap(mMatrix);//creating bitmap of code imageCode.setImageBitmap(mBitmap);//Setting generated QR code to imageView // to hide the keyboard InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); manager.hideSoftInputFromWindow(etText.getApplicationWindowToken(), 0); } catch (WriterException e) { e.printStackTrace(); } }); } } |