EDIT: updated my code , posted more of the code for you to see . EDIT: why am i being downvoted ? EDIT2: broke my code lol , im going to reverse all you're suggestions , and try the xml option provided
i would like to know if i can use startActivityForResult
in my main activity.
i'm opening a scannerview
with the code below ,
what i see as my SECOND activiy
.
how could i get that result because it's not officially another activity it's only a method.
Button sendButton;
//EditText edt4;
EditText edt2;
@SuppressLint("CutPasteId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt4 = findViewById(R.id.editText4);
ZXingScannerView mScannerView = findViewById(xmlScannerView););
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
public void onClick(View v){
ZXingScannerView mScannerView = new ZXingScannerView(this);
mScannerView.setVisibility(View.VISIBLE);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
//EditText editText4;
EditText edt4;
@Override
public void handleResult(final Result result) {
//handle result
Log.v("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Ordernummer of Locatie");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
//edt4.setText(result.getText());
//edt4 = findViewById(editText4);
//edt4.setText(String.valueOf(result.getText()));
runOnUiThread(new Runnable() {
@Override
public void run() {
updateScannerData(1,result.getText());
}
});
}
private void updateScannerData(int scanType, String scannedCode){
//startActivity(new Intent(this,MainActivity.class));
//this.finish();
edt4.setText(scannedCode);
}
@Override
public void onBackPressed()
{
startActivity(new Intent(this,MainActivity.class));
this.finish();
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="@mipmap/ic_launcher_foreground">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="@string/scan_locatie"
android:inputType="text"
android:text=""
tools:backgroundTint="@android:color/holo_red_light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText4"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_red_light"
android:onClick="onClick"
android:text="@string/scan_qr"
tools:text="scan qr code" />
<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=""
android:visibility="visible"
tools:backgroundTint="@android:color/holo_red_light" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_red_light"
android:onClick="onClick"
android:text="@string/scan_qr"
tools:text="scan qr code" />
<Button
android:id="@+id/sendButton"
android:layout_width="157dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:background="@android:color/holo_red_light"
android:text="@string/button"
tools:text="Versturen.." />
<Button
android:id="@+id/button3"
android:layout_width="40dp"
android:layout_height="38dp"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignParentEnd="true"
android:background="@android:drawable/ic_delete" />
<Button
android:id="@+id/button4"
android:layout_width="39dp"
android:layout_height="37dp"
android:layout_alignBaseline="@+id/editText4"
android:layout_alignParentEnd="true"
android:background="@android:drawable/ic_delete" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="@+id/xmlScannerView"
android:visibility="gone"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</FrameLayout>
See Question&Answers more detail:os