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();
}
}