Example taken from here and there is the answer
My fragment class:
public class ExciseBarcodeScanFragment extends AbstractFragment {
private Button button;
private CompoundBarcodeView barcodeView;
@Override
public int getTitleRes() {
return R.string.validation_of_excise_stamps_on_alcohol_products;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_excise_barcode_scan, container, false);
init(rootView);
return rootView;
}
private void init(View rootView) {
// ...
barcodeView = (CompoundBarcodeView) rootView.findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
}
private BarcodeCallback callback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
if (result.getText() != null) {
barcodeView.setStatusText(result.getText());
}
//Do something with code result
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
@Override
public void onResume() {
barcodeView.resume();
super.onResume();
}
@Override
public void onPause() {
barcodeView.pause();
super.onPause();
}
}
Fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp">
<com.journeyapps.barcodescanner.CompoundBarcodeView
android:id="@+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="30dp">
<Button
android:id="@+id/manuallyButton"
style="@style/SearchField.Button"
android:text="@string/search"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
I know that comes as a duplicate, but it does not work. I'm new, I'm sorry if the horrible formatting. Please help.