Step by Step Implementation
- Open Android Studio and select "Create New Project".
- Name the project as per your wish and select your activity template.
- Click the “Finish” button to create a new project in Android Studio.
Step 2: Add dependency to build.gradle(Module:app)
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation ‘androidmads.library.qrgenearator:QRGenearator:1.0.3’
Now sync the project from the top right corner option of Sync now.
Step 3: Working with the 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.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--We are using this image
view to display our QR code-->
<ImageView
android:id="@+id/idIVQrcode"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:contentDescription="@string/qr_code" />
<!--Edit text to enter text
for creating a QR code-->
<EditText
android:id="@+id/idEdt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idIVQrcode"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="20dp"
android:autofillHints=""
android:hint="@string/enter_your_info"
android:inputType="text" />
<!--Button for creating a QR code-->
<Button
android:id="@+id/idBtnGenerateQR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdt"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="20dp"
android:text="@string/generate_qr_code" />
</RelativeLayout>
Step 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.WriterException;
import androidmads.library.qrgenearator.QRGContents;
import androidmads.library.qrgenearator.QRGEncoder;
public class MainActivity extends AppCompatActivity {
private ImageView qrCodeIV;
private EditText dataEdt;
private Button generateQrBtn;
Bitmap bitmap;
QRGEncoder qrgEncoder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
qrCodeIV = findViewById(R.id.idIVQrcode);
dataEdt = findViewById(R.id.idEdt);
generateQrBtn = findViewById(R.id.idBtnGenerateQR);
generateQrBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(dataEdt.getText().toString())) {
Toast.makeText(MainActivity.this, "Enter some text to generate QR Code", Toast.LENGTH_SHORT).show();
} else {
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int width = point.x;
int height = point.y;
int dimen = Math.min(width, height);
dimen = dimen * 3 / 4;
qrgEncoder = new QRGEncoder(dataEdt.getText().toString(), null, QRGContents.Type.TEXT, dimen);
try {
bitmap = qrgEncoder.encodeAsBitmap();
qrCodeIV.setImageBitmap(bitmap);
} catch (WriterException e) {
Log.e("Tag", e.toString());
}
}
}
});
}
}