QR Code Scanner Application using Zxing

Its been a few weeks since I created this blog but could not decide what to post first. There was so much to share, too many options. Finally, here’s my first blog post. I hope you find it useful.

A few days back I was trying to Integrate ZXing QR Code  into my android app without having to install BarCode Scanner app. A lot of tutorials I found on the internet went with the installation of BarCode Scanner app which opened BarCode Scanner whenever a scan was required.

However, a much elegant way is to implement a built-in scanner. The easiest way according to my experience is using Zxing Android Embedded. The method requires nothing extra-complicated and it worked for me perfectly. Here’s the link to the complete working project on github.

This tutorial guides you through the steps to create a basic QR scanning app on Android Studio, which displays the scanned result in a dialog box. There are 7 simple steps. Lets get started.

Step 1

Create a new project with Blank Activity.


Step 2

We start by setting up our environment.  First step, adding Zxing library as a dependency of our project. Assuming we do not know the package name we require right now, we can search for it. Go to File > Project Structure > Dependencies > + > Library Depedency.



Search for “zxing” and select the zxing-parent library. Enter, and you should be able to see it added in your project.



“Apply” the changes and close the window. To verify the dependency has been added, open the “build.gradle” file.


A similar and simpler way to add a dependency is to add the highlighted line in “build.gradle(app)” file.

Step 3

Now to add the Zxing Android Embedded library to our project, we add the following:

compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'

This is what the gradle file finally looks like:


Step 4

For the final step in setting up the dependencies, download core.jar and add it in the “libs” folder of the project.


Step 5

The setup is now complete. All that remains now is the code. Add a button to the xml UI file. We’ll use this button to start the scanning activity.

 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Scan Code" android:id="@+id/button_scan" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:onClick="startScan"/> 


Step 6

Now we come towards editing the MainActivity.java to add the event handler of button click.

public void startScan(View sender) {

IntentIntegrator integrator = new IntentIntegrator(this);


For more information on IntentIntegrator and other Zxing components, visit this.

I decided to display my result in a dialog box. To decide what we do with the result, we implement the “onActivityResult” method.

public void onActivityResult(int request, int result, Intent i) {

IntentResult scan = IntentIntegrator.parseActivityResult(request, result, i);

Context context = getApplicationContext();

if (scan != null) {

if (result == RESULT_OK) {

String contents = i.getStringExtra("SCAN_RESULT"); //this is the result of scan

AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);


dlgAlert.setTitle("Code Scanned");


new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog here

} else if (result == RESULT_CANCELED) {

Toast toast = Toast.makeText(context, "No result", Toast.LENGTH_LONG);



Step 7 (optional)

I recommend cleaning the project once after adding external dependencies to avoid any chances of time wastage. The app is now ready.








Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s