Android Development 103: Barcode Scanner!

Ok let’s finally do something useful! Of course this has been done a million times, but what the heck, let’s make a barcode scanner.

First off, we are going to build it with Google’s ZXing (zebra crossing) library. This allows us to open the camera, use the app they already built and capture the data without us having to write our own barcode library. Here it goes:

Let’s make our layout file (under App -> res -> layout -> activity_main.xml):

<LinearLayout xmlns:android=""
	android:orientation="vertical" >

    <Button android:id="@+id/button_scan"
        android:text="@string/button_scan" />

	    android:textIsSelectable="true" />

	    android:textIsSelectable="true" />


Then we need to populate some strings for those files (App -> res -> strings.xml):

<?xml version="1.0" encoding="utf-8"?>

    <string name="app_name">Barcode</string>
    <string name="action_settings">Settings</string>
    <string name="button_scan">Scan</string>
    <string name="scan_format">Scan Format</string>
    <string name="scan_content">Scan Content</string>


Now we need to write out app. We are going to include the google Zxing library. To do that right click the “src” directory. Then select “New” -> “Package”. Enter “” for the “Name”

Now we need to add some classes. Download the latest zxing library ( and unzip it. Navigate to the file (zxing-2.2 -> android-integration -> src -> com -> google -> zxing -> integration -> android -> and open the file in your favorite text editor. Copy the file contents.

Create a new class in your newly created package (right click your package and select “New” -> “Class”). Name your file “” and paste in the contents of the library file.

Create another new class named “” and paste the contents of in the downloaded library file.

Now in MainActivity (App -> src -> com.example.barcode ->

package com.example.barcode;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

	private Button buttonScan;
	private TextView textFormat, textContent;

	private static String logtag = "barcode";

	protected void onCreate(Bundle savedInstanceState) {

		buttonScan = (Button)findViewById(;
		textFormat = (TextView)findViewById(;
		textContent = (TextView)findViewById(;


	private OnClickListener scanListener = new OnClickListener() {
		public void onClick(View v) {
			if(v.getId() == {
				// we know we are talking about the button
				IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

		Log.d(logtag, "able to set the text");

		if(scanResult != null) {
			Log.d(logtag, "able to set the text");

			String scanContent = "Scan Content: ";
			String scanFormat = "Scan Format: ";

			scanContent += scanResult.getContents();
			scanFormat += scanResult.getFormatName();

			Log.d(logtag, "able to set the text");

			// put the results to the text

		} else {
			Toast.makeText(MainActivity.this, "we didn't get anythign back from the scan", Toast.LENGTH_SHORT).show();

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(, menu);
		return true;


This will open the camera, scan for a barcode, and return the information about the barcode to you. I’ll post a video of how to implement this in a second.



Leave a Reply

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