Android Barcode Reader Example Tutorial

android barcode reader

To provide ease and leisure to Android users, there are millions of apps available on play store. Android barcode reader is an app which allows users to read barcode. This tutorial explains how you can make a barcode reader app for Android.

What is a Barcode Reader?

A barcode reader is an electronic device which can read barcode and output on a displaying device like computer or Android. With the help of Android barcode reader you can scan barcodes on products. There are hundreds of android barcode reader apps available on play store. Now you can make your own just follow this tutorial. It’s very easy as Google has launched a free barcode reader library called ZXing that can be accessed via an Intent in app.

Android Barcode Reader Example

Every android device has the ability to read barcode to decode a lot of information. This tutorial is a complete guide to make an app in android for barcode reader. We will implement Android barcode reader app with the use of ZXing (Zebra Crossing) library, which will use to scan barcode in Android.
ZXing (Zebra Crossing) is an open source, 1D/2D image processing library implemented in Java.

Go to here for ZXing library

Things you need to develop android barcode reader

  1. Android Studio
  2. Android device
  3. USB cable
  4. A product with barcode
  5. ZXing library

Now follow these steps  to create a new Project

Open Android Studio go to File => New Project

Enter your application name and select your project directory/path/ location

Click next, leave default settings as it is and finish the process.

Download ZXing package and include it in your project’s build path.

Create a Blank Activity called Main Activity

Go to res folder open acitivity_main.xml

Switch to design view

Create linear layout, a button( to start reading barcode) and a text view (to display information) in this activity

Here is the code for activity_main.xml

<? xml version = "1.0" encoding = "utf-8" ?>
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns:android = ""
    xmlns:app = ""
    xmlns:tools = ""
    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"
    app:layout_behavior = "@string/appbar_scrolling_view_behavior"
    tools:context = "com.example.admin.barcodereader.MainActivity"
    tools:showIn = "@layout/activity_main" >

    < LinearLayout
        android:orientation = "vertical"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:layout_centerVertical = "true"
        android:layout_alignParentLeft = "true"
        android:layout_alignParentStart = "true"
        android:weightSum = "1" >

        < TextView
            android:layout_width = "192dp"
            android:layout_height = "wrap_content"
            android:layout_marginTop = "20dp"
            android:text = "SCAN"
            android:textAlignment = "center"
            android:textSize = "30dp"
            android:id = "@+id/textView"
            android:layout_gravity = "center_horizontal"
            android:layout_weight = "0.21" />

        < Button
            android:layout_width = "187dp"
            android:layout_height = "wrap_content"
            android:text = "Bar Code"
            android:textSize = "20dp"
            android:id = "@+id/button"
            android:layout_gravity = "center_horizontal"
            android:layout_weight = "0.08"
            android:onClick = "ScanBar" />
    </ LinearLayout >
</ RelativeLayout >

So now we have to code it, implement onClick() function in class

To implement this code we need Zebra Cross library  at

Here is the complete code of

import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    static final String SCAN = "";
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        Toolbar toolbar = ( Toolbar ) findViewById( );
        setSupportActionBar( toolbar );

        FloatingActionButton fab = ( FloatingActionButton ) findViewById( );
        fab.setOnClickListener( new View.OnClickListener() {
            public void onClick( View view ) {
                Snackbar.make( view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction( "Action", null ).show();

    // fucntion to scan barcode
    public void ScanBar ( View view ) {
        try {
            //this intent is used to call start for bar code
            Intent in = new Intent( SCAN );
            in.putExtra( "SCAN_MODE", "PRODUCT_MODE" );
            startActivityForResult( in, 0 );
        } catch ( ActivityNotFoundException e) {
            showDialog( MainActivity.this,"No scanner found", "Download Scanner code Activity?"," Yes", "No" ).show();


    private Dialog showDialog ( final Activity act, CharSequence title,CharSequence message, CharSequence yes, CharSequence no ) {

        // a subclass of dialog that can display buttons and message
        AlertDialog.Builder download = new AlertDialog.Builder( act );
        download.setTitle( title );
        download.setMessage ( message );
        download.setPositiveButton ( yes, new DialogInterface.OnClickListener ( ) {
            public void onClick( DialogInterface dialog, int i ) {
                // TODO Auto-generated method stub
                //uri to download barcode scanner
                Uri uri = Uri.parse( "market://search?q=pname:" + "" );
                Intent in = new Intent ( Intent.ACTION_VIEW, uri );
                try {
                    act.startActivity ( in );
                } catch ( ActivityNotFoundException e) {

            download.setNegativeButton ( no, new DialogInterface.OnClickListener() {
                public void onClick ( DialogInterface dialog, int i ) {
                    // TODO Auto-generated method stub

    protected void onActivityResult ( int requestCode, int resultCode, Intent in ) {
        // TODO Auto-generated method stub
        if( requestCode == 0 ){
            if( resultCode == RESULT_OK ){
                //use to get scan result
                String contents = in.getStringExtra( "SCAN_RESULT" );
                String format =  in.getStringExtra( "SCAN_RESULT_FORMAT" ) ;
                Toast toast = Toast.makeText( this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG );

After completing code part now run your application on emulator to check but it will not work for emulator as emulator does not has any scanner device.

So attach a USB cable and build apk file for your Android device. It will work fine for your Android device.

By importing the ZXing integration classes into our app, we can make user scans easier and focus our development efforts on handling the scan results.

In conclusion

In this tutorial we explained how to create a barcode scanner application for your android device. Since we used ZXing library so don’t need to install barcode scanner in our device, ZXing will do this job for us.

There are many other ways to create a barcode reader app but this one is simplest of all. Now you can scan, decode any product’s barcode.

You can use this feature in many other applications, often in building complex Android apps, we need to read a barcode, and you can integrate this code in your application.


One Response

  1. Avatar

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.