Search code examples
javaandroidbarcode-scanner

Getting a value and displaying it on an EditText


I have the following code, It's a barcode scanning App using the ZXing Library.

txtoperatrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("Scannez le code à barres SVP");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(true);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });
}

txtoperatriceis the EditText where the the barcode result should be displayed. The result is got by the following code :

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result!=null){
        if(result.getContents()==null){
            Toast.makeText(this, "Erreur de Scan", Toast.LENGTH_LONG).show();
        }
        else{
            ///////// DISPLAY THE CODE IN THE EDITTEXT txtoperatrice
        }
    }
    else{
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Solution

  • make txtoperatrice a global variable and then assign it via findViewById on your onCreate Method

    and onActivityResult set its text to the result using the following code

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result!=null){
            if(result.getContents()==null){
                Toast.makeText(this, "Erreur de Scan", Toast.LENGTH_LONG).show();
            }
            else{
               txtoperatrice.setText(result.getContents());
            }
        }
        else{
            super.onActivityResult(requestCode, resultCode, data);
        }
    }