Hey guys in this tutorial we will learn creating Android QR Code Scanner. So if you need something like a Barcode or QR Code Scanner in your android application, then you are at the right place. So lets start creating our Android QR Code Scanner.
Table of Contents
What is a QR Code?
You may already seen QR Code many times. Whatsapp web also uses QR Code for login. QR Code stands for Quick Response Code. QR Code is actually a 2 dimensional bar code. A QR Code consist of black squares over a white background, and it can store some data, which can be read by camera or other imaging devices. Below you can see a sample QR Code.

Generating a QR Code
To scan a QR Code the first thing needed is itself a QR Code. So for this tutorial we will generate a Simple QR Code consisting of Name and Address. Below is the QR Code I already generated a free service.

To generate your own QR Code you can go to this link. I have given JSON string as QR Data you can see in the below screenshot.
Creating Android QR Code Scanner
The following project will work for Barcode as well.
Creating a new Project
- As always create a new Android Studio project.
- I just created AndroidQRCodeScanner.

Adding Zxing Library
- For reading QR Code we will be using Zxing Library.
- So once your project is loaded go to your app level build.gradle file and add the following dependency.
1 2 3 4 5 6 7 8 9 10 11 12 13 | dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.1' testCompile 'junit:junit:4.12' //add this dependency compile 'com.journeyapps:zxing-android-embedded:3.4.0' } |
- Now sync your project with gradle.
Creating User Interface
- We have two values (Name, Address) in the QR Code that we generated. So I designed the following layout that will display Name and Address. We also have a button in the layout that will open up camera for scanning the QR Code.

- For designing the above layout you can use the following xml code.
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 46 47 48 49 50 51 52 53 54 55 | <?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="net.simplifiedcoding.androidqrcodescanner.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name" /> <TextView android:id="@+id/textViewName" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="xyz" android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Address" /> <TextView android:id="@+id/textViewAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="xyz" android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> </LinearLayout> <Button android:id="@+id/buttonScan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Scan QR Code" /> </RelativeLayout> |
Building Android QR Code Scanner
Defining View Objects
- Now come inside MainActivity.java and first define the view objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class MainActivity extends AppCompatActivity { //View Objects private Button buttonScan; private TextView textViewName, textViewAddress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //View objects buttonScan = (Button) findViewById(R.id.buttonScan); textViewName = (TextView) findViewById(R.id.textViewName); textViewAddress = (TextView) findViewById(R.id.textViewAddress); } } |
- Now we will attach OnClickListener to button.
Attaching OnClickListener
- Now we will attach OnClickListener to our button.
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 | //implementing onclicklistener public class MainActivity extends AppCompatActivity implements View.OnClickListener { //View Objects private Button buttonScan; private TextView textViewName, textViewAddress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //View objects buttonScan = (Button) findViewById(R.id.buttonScan); textViewName = (TextView) findViewById(R.id.textViewName); textViewAddress = (TextView) findViewById(R.id.textViewAddress); //attaching onclick listener buttonScan.setOnClickListener(this); } @Override public void onClick(View view) { } } |
Android QR Code Scanner
- Now its time to scan the QR Code.
- For scanning modify the code of MainActivity.java as below.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package net.simplifiedcoding.androidqrcodescanner; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; import org.json.JSONException; import org.json.JSONObject; //implementing onclicklistener public class MainActivity extends AppCompatActivity implements View.OnClickListener { //View Objects private Button buttonScan; private TextView textViewName, textViewAddress; //qr code scanner object private IntentIntegrator qrScan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //View objects buttonScan = (Button) findViewById(R.id.buttonScan); textViewName = (TextView) findViewById(R.id.textViewName); textViewAddress = (TextView) findViewById(R.id.textViewAddress); //intializing scan object qrScan = new IntentIntegrator(this); //attaching onclick listener buttonScan.setOnClickListener(this); } //Getting the scan results @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null) { //if qrcode has nothing in it if (result.getContents() == null) { Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show(); } else { //if qr contains data try { //converting the data to json JSONObject obj = new JSONObject(result.getContents()); //setting values to textviews textViewName.setText(obj.getString("name")); textViewAddress.setText(obj.getString("address")); } catch (JSONException e) { e.printStackTrace(); //if control comes here //that means the encoded format not matches //in this case you can display whatever data is available on the qrcode //to a toast Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show(); } } } else { super.onActivityResult(requestCode, resultCode, data); } } @Override public void onClick(View view) { //initiating the qr code scan qrScan.initiateScan(); } } |
- Now you can try running your application.

- Bingo! Our Android QR Code Scanner is working absolutely fine.
So thats all for this Android QR Code Scanner Tutorial friends. Feel free to leave out your comments if having any confusions or queries. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.
Thank you so much.
Thank you mate
Great tutorial! Can you kindly show me how to open the qr code within a fragment and with the camera not at full screen but only the middle part of the fragment is the camera
Very Nice 🙂
Appreciated.
sir please make the scanner view with square shape and delete the red horizontal line in between the scanner view.
I need this requirement sir.please post it…
https://github.com/journeyapps/zxing-android-embedded#usage-with-intentintegrator. Go to this link which is the Github page for used library. Read about use from Fragment and Customize options. I hope you will find your answer.
Atlease post the code how to remove the red horizontal line on top of Scanner view.
awesome man!!!
you ROCK <3. its working fine. just i wanted to send this result data in an edittext instead of showing result in a toast.
anyone!!! plz help me in this.
what if I wanted to retrieve data from SQL database, the QR code contains an ID number that is unique in the database and I want to retrieve that specific record by scanning the ID .. can you help me, please?
Did u get any soln for ur query?
I m stuck with same thing.
If u have any idea pleaselet me know asap
Thank you sir! Appreciated 😀
From Bandung, West Java, Indonesia.
Hi Sir,
I am Getting Error
Error:Execution failed for task ‘:app:transformClassesWithDexForDebug’.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘C:\Program Files\Java\jdk1.7.0_79\bin\java.exe” finished with non-zero exit value 1
Excellent..!!!! Working as a charm
Thanks so much, but i have a trouble , the scan time its so slow, do you have any solutions?
i want to fetch textview scan result and match with my localhost data how i can do this kindly give me code i have no idea
Hi, Wonderful explanation.Can u write a post for multiple QR scanning.And get result to array??
Please inbox me at sraghudatta94@gmail.com
Thank you so much. it works for me.
Hi can you help me, to scan QR code using front camera?
whai is name in object and direct go in catch block to scan
Awesome and helpful article
Thank you Belal, but it’s only work in landscape orientation is’n it?
How to change the orientation to potrait?
just paste this code into your android manifest.
the camera and app turns off after detecting qr code. It started to happen after upgrading the android studio version. 🙁
I am having an error on this part of the code:
“import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;”
the ‘integration’ part. Can someone please help me? Pleeeaasseee T.T
In mobile this sample is not working.Please help me.its not scaning QR code/bar code.
hi bro,
How to change the orientation for the scanner
hi i am invoke the coding is working perfectly so next how to open flash light for barcoding scanner in adroid please help me its very urgent guyes
How can i change the orientation to portrait mode?
what code to paste?
ScreenOrientation= “Portrait”
is not working in manifest file
How can I do this in Portrait Mode ?
Have you got any idea scanning in Portrait mode ?
Please help me on this.
this work good.
but when i scan other QR then always print in TOAST so any other way to scan all QR and get data in TextView ?
Hi…the code is running perfectly fine when on the main activity, I was wondering how will I be able to call this activity from another activity on button click. I have tried using Intent but it does not seem to work and the app crashes, do you know why this is happening and how to solve it?
hello. i love your work. it works on me. thumbs up. i was wondering if the qr code scanning can fetch data from mysql . instead of displaying data, i want to scan the qr code and when it (QR CODE = QR CODE) something like this. it display all the data from mysql (SELECT * FROM data WHERE QRCODE = QRCODE – php file) when scanned. and it will display in listview . thanks. i hope you read this. and i hope you will help me. thank you very much. its for thesis thing. we are making an mobile pos system using qr code scanning. thank you very much
Hi guys, do you have a working apk file for this, kindly send me download link. Many Thanks.
excellent explanation sir… thanks
I try to edit the code given above. how to display the URL of website on the app using qr code scanner?
Can you give direction on how to take the data we get from scanning, and save it to a mysql database?
what if I wanted to retrieve data from SQL database, the QR code contains an ID number that is unique in the database and I want to retrieve that specific record by scanning the ID .. can you help me, please?
please help me as soon as possible …..
It is working for Bar Code but not for QR Code..
Hey, I am using this code but it is taking near about 20 secs to scan Bar code, can you please help me to read bar code fast?
where we put the json file in the java portion or the xml portion ..
??
https://github.com/fanrunqi/ZxingView
may be this can help you
only need to add a few lines code to get the ability.
i have two button to get qrcode but problem is how to identify the requestCode.
and how i can pass the request code
This will work for scanning a single QRCode and this is working well. Do You have a code for continuous scanning of QRCodes.
Thank you.
hello sir, i want to make scanner application to scan the QR code.