Scan Barcode And QRcode Programmatically In Android Studio

In scan barcode and QRcode programmatically in android tutorial, we will implement scan functionality in our android app programmatically.

In the end, our Android app will be able to scan both Barcode as well QRcode.

Check out what will be final result in the following video output.

Try Our App

Download DemoNuts app to learn anytime, anywhere: DemoNuts Android App

How to get maximum benefit from scan scan barcode and QRcode programmatically in android example

Download Source Code

Try Our App

Download DemoNuts app to learn anytime, anywhere: Click to get Demonuts App

Or search “demonuts” in play store from android mobile device.

Creating scan barcode and QRcode programmatically in android example

If you are using adblocker, then you will not be able to show our site smoothly. For example, you find step 6 after step 1 or similar to this. So please disable adblocker to fully utilize all resources of DemoNuts

Step 1: Create a new project in Android Studio.

Step 2: Updating build.gradle(Module:app) file

Add following code into dependencies{} 

Here we are using ZBar, but you can also use ZXing.

For using ZXing library follow this : zxing barcode qrcode scanner android

Github link for ZBar or ZXing library : ZXing or ZBar

Step 3: Updating AndroidManifest.xml file

add camera permission between <manifest>….</manifest> tag.

Note: If you are targeting SDK version above 22 (Above Lollipop)  then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.

Step 4: Updating activity_main.xml file:

Copy following code into android_main.xml file:

Step 5: Updating file:

Update as per below code:

Here we will open ScanActivity on the button click.

Step 6: Opening new activity in android studio

To open new activity in android studio, click on File tab which is present at the left top bar.

Now go to File->New->Activity->Empty Activity and give name of activity as ScanActivity.

scan barcode qrcode android

open new activity

Step 7: Updating file:

Add below code to

Note : We do not need to update activity_scan.xml file because we will open camera preview here so no need to do anything in layout file.

Step 8: Brief Description of

  • In onCreate() method, we have set view as scannerview instead of xml file layout.
  • In onResume() method, we have set the resulthandler and started camera preview.
  • In onPause() method, we are stopping camera preview.

Step 9: Explaining handleResult() method

  • As you can see in above code, we get result object which contains resulting Barcode or QRcode after scanning is completed.
  • We will get final result by running result.getContents() method and we have set it into TextView of MainActivity.

How to get exact Android example while searching in Google

When you search for any Android example in Google, always add “Android Studio programmatically” at the end of your query.

Why add studio?

Click to get Important Google searching tips

So it was all about scan barcode and QRcode programmatically in android studio example. Thank you 🙂

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

6 Responses

  1. cesar edinson says:

    Many thanks … excellent posting … and they work correctly .. Greetings from Peru

  2. enrique hernandez says:

    Thanks a lot Hardik, i had many troubles with other tutorial.
    Your tutorial is short but works fine.
    Greetings from México

  3. Arindu says:

    Thanks for this code.Great work.But how to automatically provide the permission for camera ?

  4. Nehal says:

    Hello sir

    when i run this application in real device….My application is getting stopped unfortunately.

Leave a Reply

Your email address will not be published. Required fields are marked *