Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
112 views
Welcome To Ask or Share your Answers For Others

1 Answer

No you can't, either use multiple layout groups in your activity's layout or use multiple fragments.

if you go for the first option, you should add the ZXingScannerView in you XML layout and simple tangle it's visibility if you want to use it

<me.dm7.barcodescanner.zxing.ZXingScannerView
android:width="match_parent"
height="match_parent"
android:id="someId"
android:visibility="gone"
/>

And then in your code

         private ZXingScannerView mScannerView;

            @Override
            public void onCreate(Bundle state) {
                super.onCreate(state);
                setContentView(someLayout);    

                mScannerView = findViewById(SomeId);     
    mScannerView.setFormats(ZXingScannerView.ALL_FORMATS);// dont forget this
mScannerView.setResultHandler(this);
            mScannerView.startCamera()
            }
            public void onClick(View v){
              mScannerView.setVisibility(View.Visible);


            }

EDIT Call setFormats see above and start the camera is the onCreate() and change the visibility in the XML to INVISIBLE

EDIT 2

Your XML is supposed to be like this

     <FrameLayout 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: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">

    <me.dm7.barcodescanner.zxing.ZXingScannerView

        android:id="@+id/xmlScannerView"
        android:visibility="gone"
        android:layout_height="match_parent"
        android:layout_width="match_parent" /> 
    <RelativeLayout

        android:id="@+id/someId"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <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" />

    </RelativeLayout>
</FrameLayout>

And then when calling the onClick

  public void onClick(View v){

              yourRelativeLayout.setVisibility(View.Invisible);
              mScannerView.setVisibility(View.Visible);


            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...