Android barcode reader example

In this tutorial we will learn how to implement a barcode reader application in android. Barcode reader is a device which is used to scan barcodes in products and display information about the product. Here in this application we will learn how to create a barcode reader which is able to scan barcodes in products using an android application using zxing library.


  • Create project in android studio
  • Configure gradle file by adding necessary libraries.
  • Implement the layout file
  • Develop code for scanning barcode and displaying product information

Create project in android studio

Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio.

Configure gradle file

Add the following repository above dependencies

repositories {

And compile the following line inside the dependencies

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

Implement the layout file

In this step we need to add the following code to your layout file. You can find the layout file under res/layout if an activity is automatically created. If not you can create and activity by yourself. Following is the code for layout file which will be having a simple button for opening barcode scanner.


android:text="Click here to Scan BarCode"
android:textColor="#FFFFFF" />


Developing code for scanning barcode

Now you just need to add the following code to your activity’s java file . Note the in this example after scanning the barcode app will display the result as Toast message. You can set it to textview or any other widget based on your requirement.

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

public class MainActivity extends AppCompatActivity {

Button btn_scan;

protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView(R.layout.activity_main );

btn_scan = (Button) findViewById(;
btn_scan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);

protected void onActivityResult ( int requestCode, int resultCode, Intent in ) {

IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, in);

if (scanningResult != null) {

String contents = in.getStringExtra( "SCAN_RESULT" );
String format = in.getStringExtra( "SCAN_RESULT_FORMAT" ) ;
Toast.makeText(MainActivity.this, "Content-" + contents + " Format-" + format, Toast.LENGTH_LONG).show();


Note that you need to add the following permission in AndroidManifest.xml file

<uses-permission android:name="android.permission.CAMERA" />
android:required="true" />

And the following meta data under your application tag in AndroidManifest.xml file

android:value="barcode" />



About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

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