Search code examples
androidzxing

Setting a TextView after Zxing scan is not doing anything


I have a simple app that I am trying to get working. The problem is I can't get the TextView to display after its been set. I use Zxing to scan a barcode and I can set the TextView however it does not display on my phone. It just displays the default value.

When I say "set" i mean after running textview.setText() the value of the textview does change however the value on the screen does not change.

I have added some comments to help you understand where the app is at.

scanCode is where I am trying to set the textview. I have also tried using runOnUiThread and still had the same results

import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;

import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity {

TextView itemNameTextView;
TextView itemPriceTextView;
Button scanButton;

ZXingScannerView scannerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    itemNameTextView =  findViewById(R.id.itemNameTextView);
    itemPriceTextView = findViewById(R.id.itemPriceTextView);
    scanButton = findViewById(R.id.scanButton);
    scannerView = new ZXingScannerView(this);


}

public void scanCode(View view){

    setContentView(scannerView);
    scannerView.startCamera();

    scannerView.setResultHandler(new ZXingScannerView.ResultHandler() {
        @Override
        public void handleResult(Result result) {
            setContentView(R.layout.activity_main);

            //I have the correct Result here
            Log.i("Test ScanResult", String.valueOf(result)); 

            //correct Default textview value stored here
            Log.i("Test Mainactivity", itemNameTextView.getText().toString());

            //Set the textview
            itemNameTextView.setText(String.valueOf(result));

            //Sets the TextView correctly just does not display on screen
            Log.i("Test Mainactivity", itemNameTextView.getText().toString());



        }
    });


}


@Override
protected void onPause() {
    super.onPause();
    scannerView.stopCamera();
}

}


Solution

  • In your code you are calling the setContentView(scannerView); in scanCode(View view) method which will set scannerview as your Activity Layout and your previous layout which is set in onCreate() method is invalidated and your texView will not be updated.

    This is the right way to implement a ZXING Scanner

    setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }
    });
    
     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
    }
    

    Here is the Complete example

    Github Link