0

I'm doing a code which has fragments and one of the fragment job is to scan a barcode and display the contents but currently it won't display anything

I debugged and the activityResult is not being called at all

Here is the code

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;


/**
 * A simple {@link Fragment} subclass.
 */
public class PriceFragment extends Fragment{
    public PriceFragment() {
        // Required empty public constructor
    }

    Button scan_btn;
    TextView barcode;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_price, container, false);
        scan_btn = (Button) view.findViewById(R.id.scan_button);
        //initialize the textViews
        barcode = (TextView)view.findViewById(R.id.barcodeResult);
        final Activity activity = getActivity();
        scan_btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
                integrator.setPrompt("Scan");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(false);
                integrator.setBarcodeImageEnabled(true);
                integrator.initiateScan();
            }
        });

        return view;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            barcode.setText(result.getContents());
        }else {
            Toast toast = Toast.makeText(getContext(),"No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }
Draken
  • 3,134
  • 13
  • 34
  • 54
Jason
  • 51
  • 8

2 Answers2

0

I never work with zxing, but generally, if you want to catch on activity result in your fragment you need to call... fragment.startActivityForResult(...). IntentIntegrator()... probably don't do this cause you give im your activity.

You can try to:

  1. can you give im your fragment?
  2. if you override onActivityForResult. on the activity who contain your fragment will it work?

But I will suggest that you use this google's library for barcode reader.

Shreyash S Sarnayak
  • 2,309
  • 19
  • 23
JCDecary
  • 557
  • 1
  • 7
  • 18
  • alright bubt the tricky part is putting it into a fragment......but can i actually link a activity which not a fragment to a fragment? sorry still new to all these java and android – Jason Jan 08 '17 at 13:21
0

According to the documentation You are doing almost everything properly. However, there is one more thing needed:

your Activity must implement the method Activity.onActivityResult(int, int, Intent)

You are initialising IntentIntegrator with your Activity:

IntentIntegrator integrator = new IntentIntegrator(activity);

The result is passed to the Activity and not the Fragment. All you need to do is pass the result to the appropriate Fragment, as described here.

Be aware, that the method you are using requires existance of the external application installed on the phone that can handle such Intent (as the documentation states). If you would like to handle scanning inside your app internally, use [barcodescanner] (https://github.com/dm77/barcodescanner) library. The usage is described here

Community
  • 1
  • 1
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90