Skip to content Skip to sidebar Skip to footer

Can I Use Startactivityforresult , With One Activity?

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 t

Solution 1:

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;

            @OverridepublicvoidonCreate(Bundle state) {
                super.onCreate(state);
                setContentView(someLayout);    

                mScannerView = findViewById(SomeId);     
    mScannerView.setFormats(ZXingScannerView.ALL_FORMATS);// dont forget this
mScannerView.setResultHandler(this);
            mScannerView.startCamera()
            }
            publicvoidonClick(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);


            }

Solution 2:

Well, I think we need to take a look at your current code and the ZXingScannerView. If we take a look at the Github repository of the ZXingScannerView, there is an example which differs a bit from your implementation.

First of all, you should call setContentView(mScannerView);, only in your Activity's onCreate() once. Right now, when you click, the entire view gets redrawn, which is not needed. You also set the resultHandler each time when you click on the button, which might cause problems as well.

This code is taken from the Github repo of the element you are using, so please make your Activity look like this.

https://github.com/dm77/barcodescanner

publicclassSimpleScannerActivityextendsActivityimplements ZBarScannerView.ResultHandler {

    private ZBarScannerView mScannerView;

    @OverridepublicvoidonCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = newZBarScannerView(this);    // Programmatically initialize the scanner viewsetContentView(mScannerView);                // Set the scanner view as the content view
    }

    @OverridepublicvoidonResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @OverridepublicvoidonPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @OverridepublicvoidhandleResult(Result rawResult) {
        // Do something with the result hereLog.v(TAG, rawResult.getContents()); // Prints scan resultsLog.v(TAG, rawResult.getBarcodeFormat().getName()); // Prints the scan format (qrcode, pdf417 etc.)// If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

Also note that you will need permissions to access the phone's camera.

Post a Comment for "Can I Use Startactivityforresult , With One Activity?"