First, design a layout for activity:
activity_scan.xml
After click "Scan Barcode or QR code" button, our app will invoke Scan Activity of Barcode Scanner application (if you haven't install it, app will open GooglePlay to get it). Code for button event:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScanActivity">
<Button
android:id="@+id/btn_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="@string/scan" />
<TextView
android:id="@+id/format"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_scan"
android:gravity="center"
android:textColor="@android:color/holo_blue_dark"
android:textSize="20sp" />
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/format"
android:gravity="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="20sp" />
</RelativeLayout>
private View.OnClickListener onClickListener() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException ex) {
ex.printStackTrace();
//if you haven't install barcodeScanner app, download it from Google Play
downloadScanBarcode();
}
}
};
}
After complete scanning code, retrieving back data in onActivityResult()
method:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
format.setText(data.getStringExtra("SCAN_RESULT_FORMAT"));
content.setText(data.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
format.setText("Press a button to start a scan.");
content.setText("Scan cancelled.");
}
}
}
And this is method which open GooglePlay to download Barcode Scanner app:
/**
* Go to GooglePlay Store and down load "ScanBarCode" app
*/
private void downloadScanBarcode() {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(intent);
} catch (ActivityNotFoundException ex) {
ex.printStackTrace();
}
}
Full ScanActivity.java code:
package com.blogspot.hongthaiit.barcode;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ScanActivity extends Activity {
private View btnScan;
private TextView content;
private TextView format;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
btnScan = (View) findViewById(R.id.btn_scan);
content = (TextView) findViewById(R.id.content);
format = (TextView) findViewById(R.id.format);
btnScan.setOnClickListener(onClickListener());
}
private View.OnClickListener onClickListener() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException ex) {
ex.printStackTrace();
//if you haven't install barcodeScanner app, download it from Google Play
downloadScanBarcode();
}
}
};
}
/**
* Go to GooglePlay Store and down load "ScanBarCode" app
*/
private void downloadScanBarcode() {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(intent);
} catch (ActivityNotFoundException ex) {
ex.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
format.setText(data.getStringExtra("SCAN_RESULT_FORMAT"));
content.setText(data.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
format.setText("Press a button to start a scan.");
content.setText("Scan cancelled.");
}
}
}
}
Some screenshots after running (click for full size):References:
- ZXing library code project in Github: https://github.com/zxing/zxing
- Barcode Scanner app on GooglePlay: https://play.google.com/store/apps/details?id=com.google.zxing.client.android
- Reading my newer post to developing a "native" barcode/QR code application.
(sorry for having ads)