0

I've been working with zXingScannerView and I went to set the result text in my Edittext in the same Activity but it does not work!! Note:it shows in a Toast:

Toasty.success(getApplicationContext(),"Code Scaned ! :"+result.getText(),Toast.LENGTH_SHORT).show();

My Code :

public void scan(View view){
    zXingScannerView =new ZXingScannerView(getApplicationContext());
    setContentView(zXingScannerView);
    zXingScannerView.setResultHandler(this);
    zXingScannerView.startCamera();
}

@Override
protected void onPause() {
    super.onPause();
    zXingScannerView.stopCamera();
    setContentView(R.layout.activity_scan_);
}

@Override
public void handleResult(Result result) {
    setContentView(R.layout.activity_scan_);
    Toasty.success(getApplicationContext(),"Code Scaned ! :"+result.getText(),Toast.LENGTH_SHORT).show();
    Scan_Activity.this.scan.setText(result.getText());
    zXingScannerView.resumeCameraPreview(this);

}
Benoit
  • 5,118
  • 2
  • 24
  • 43
Ali Ourag
  • 92
  • 7

1 Answers1

0

Here, you only need to call setContentView() once in onCreate(), the issue is occurring because you are calling setContentView() again in handleResult().

 @Override
public void handleResult(Result result) {
    Toasty.success(getApplicationContext(),"Code Scaned ! :"+result.getText(),Toast.LENGTH_SHORT).show();
    Scan_Activity.this.scan.setText(result.getText());
    zXingScannerView.resumeCameraPreview(this);

}

Also there is not need of calling setContentView() again in onPause() because your layout is already get created when onCreate() is called.

This will work for sure. Also check this for more reference

  • i use setContentView(R.layout.activity_scan_); to get back to the scan activity ! – Ali Ourag Dec 13 '18 at 14:46
  • Just for clarification setContentView() only changes the layout of the activity , you have to call onBackPressed() to go back to scan activity. – krishna chouhan Dec 13 '18 at 14:48
  • so ,i hav my dashboard activity i i go to scan activity with using: public void click_bt1(View v){ Intent i=new Intent(this,Scan_Activity.class); startActivity(i); } – Ali Ourag Dec 13 '18 at 14:56
  • the scan activity :||public class Scan_Activity extends AppCompatActivity implements ZXingScannerView.ResultHandler{ private EditText scan,titel; private ZXingScannerView zXingScannerView; – Ali Ourag Dec 13 '18 at 14:58