Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change screen orientation automatically while scanning (using ZXING library)

I've MainActivity.kt where I show different fragments for different needs. At some point, I press button 'X' that calls startScanner() function:

private fun startScanner() {
    IntentIntegrator(this)
            .setOrientationLocked(false)
            .setPrompt("SCANNING?")
            .initiateScan()
}

Manifest.xml:

<activity
    android:name=".MainActiity"
    android:theme="@style/AppTheme"
    tools:replace="android:screenOrientation"
    android:stateNotNeeded="true"
    android:screenOrientation="fullSensor"
    android:windowSoftInputMode="stateHidden" />

Gradle.file:

compile 'com.journeyapps:zxing-android-embedded:3.6.0'

It does open scanner and everything, but in landscape mode.

Why is this not working?

like image 792
MaaAn13 Avatar asked Jul 12 '18 13:07

MaaAn13


2 Answers

There is a shortcut to do this. Just add this to the manifest:

<activity
       android:name="com.journeyapps.barcodescanner.CaptureActivity"
       android:screenOrientation="portrait"
       tools:replace="android:screenOrientation"
       android:stateNotNeeded="true"/>
like image 81
Anisuzzaman Babla Avatar answered Nov 05 '22 20:11

Anisuzzaman Babla


in addition of this answer https://stackoverflow.com/a/35465968/7666442

I found way to change Orientation of zxing scanner activity automatically when device Orientation change

Try this way

CaptureActivityPortrait

public class CaptureActivityPortrait extends CaptureActivity {
//Nothing in side.
}

CaptureActivityPortrait in manifest file

    <activity
        android:name=".CaptureActivityPortrait"
        android:stateNotNeeded="false"
        android:theme="@style/zxing_CaptureTheme"
        android:windowSoftInputMode="stateAlwaysHidden"/>

use this way in your activity

public class MyActivity extends AppCompatActivity {

    IntentIntegrator qrScan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        qrScan = new IntentIntegrator(this).setCaptureActivity(CaptureActivityPortrait.class);

        qrScan.setOrientationLocked(false);
        qrScan.initiateScan();
    }


}
like image 1
AskNilesh Avatar answered Nov 05 '22 19:11

AskNilesh