-2

I'm working on a QR scanner app. After a succesfull scan the scanned info goes into the alert box. From there, I need the info to go to an EditText field or skip the entire alert dialog. The result which has to go to the EditText is in the function below.

public class MainActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;


EditText editText;
@SuppressLint("CutPasteId")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = findViewById(R.id.editText4);


    EditText delete2;
    Button button3;



    button3 = findViewById(R.id.button3);
    delete2 = findViewById(R.id.editText2);

    final EditText finalEdittext = delete2;
    button3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //Clear EditText
            finalEdittext.getText().clear();

        }
    });

    EditText delete4;
    Button button4;

    delete4 = findViewById(R.id.editText4);
    button4 = findViewById(R.id.button4);

    final EditText finalEdittext1 = delete4;
    button4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //Clear EditText
            finalEdittext1.getText().clear();

        }
    });
}


public void onClick(View v){
    mScannerView = new ZXingScannerView(this);
    setContentView(mScannerView);
    mScannerView.setResultHandler(this);
    mScannerView.startCamera();


}

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

}



@Override
public void handleResult(Result result) {
    //handle result
    Log.v("handleResult", result.getText());


    //editText.setText(result.toString());
   // editText.invalidate();
    mScannerView.stopCamera(); //setContentView(R.layout.activity_main);
    //resume scanning
    //mScannerView.resumeCameraPreview(this);
    updateScannerData(1,result.getText());



}



public void updateScannerData(int scanType, String scannedCode){
   editText.setText(scannedCode);
   //editText.invalidate();
   //editText.requestLayout();

    //finish();
    //startActivity(getIntent());
    this.recreate();


    }

}

I only use one main activity and this is the XML of the EditText where the result has to go.

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="61dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="197dp"
    android:ems="10"
    android:hint="@string/scan_order"
    android:inputType="textPersonName"
    android:visibility="visible"
    tools:backgroundTint="@android:color/holo_red_light" />

Any help is greatly appreciated. Thanks in advance.

  • Possible duplicate of [How To Set Text In An EditText](https://stackoverflow.com/questions/4590957/how-to-set-text-in-an-edittext) – Óscar Jul 26 '18 at 08:53

2 Answers2

0

There can be more then one approach here, you can :

Edittext edt = (EditText)findViewById(R.id.viewid);
edt.setText(result.getText());

or if you need to update this text view frequently multiple times make method inside your main activity updateScannerEditText(String value); and call it from your fragment.

In case you have handleResult() inside you main activity itself simply create method inside your activity :

@Override  // move your setContentView() method inside activity oncreate()
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
}

public void handleResult(Result result) {
   //handle result
Log.v("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();

mScannerView.stopCamera();
updateScannerData(1,result.gettext()); 


//resume scanning
mScannerView.resumeCameraPreview(this);

}

Method implementation :

private void updateScannerData(int scanType, String scannedCode){
edt.setText(scannedCode);//edt is the editTextView that you have declared in you xml.
}

Hope it helps.

Sahil
  • 952
  • 1
  • 7
  • 14
0

So if i've understood right you're trying to scan a QR Code then you're setting it to an EditText2 and then you're scanning again a product and trying to set it in EditText4.

So just try to make somethink like this:

public void handleResult(Result result) {
    //handle result
    Log.v("handleResult", result.getText());

    if(edittext2.getText().equals("")){
    edittext2.setText(result.getText());
    }else{
     edittext4.setText(result.getText())
      }

    mScannerView.stopCamera(); setContentView(R.layout.activity_main);
    //resume scanning
    mScannerView.resumeCameraPreview(this);

}
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100