Search code examples
androidandroid-fragmentszxing

Android/Zxing: Zxing Scanner doesn't trigger the onActivityResult method


I'm building an android app integrating Zxing barcode scanner. The camera opens up and shows the redline but it doesn't scan the data. Here is my code for the fragment: I've used the v4.app.fragment.

public class AddItem extends Fragment{

    public AddItem() {
        // Required empty public constructor
    }
    String Idno;
    String Name;
    String Brand;
    String Cost;
    String Storeid;
    String Date;
    String Type;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add_item, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ImageButton button=(ImageButton)getView().findViewById(R.id.img);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

             IntentIntegrator.forSupportFragment(AddItem.this).setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES ).initiateScan();

            }
        });
    }


    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //retrieve scan result
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        final EditText idno=(EditText)getView().findViewById(R.id.IdNum);
        final EditText title=(EditText)getView().findViewById(R.id.title);
        final EditText brand=(EditText)getView().findViewById(R.id.brand);
        final EditText cost=(EditText)getView().findViewById(R.id.price);
        final EditText store=(EditText)getView().findViewById(R.id.storeid);
        final EditText date=(EditText)getView().findViewById(R.id.date);


        if (scanningResult != null) {
            //we have a result
            String scanContent = scanningResult.getContents();
            String scanFormat = scanningResult.getFormatName();
            Toast toast = Toast.makeText(getActivity(),scanContent, Toast.LENGTH_SHORT);
            toast.show();
            // display it on screenformatTxt.setText("FORMAT: " + scanFormat);
            idno.setText(scanContent);


        }else{
            Toast toast = Toast.makeText(getActivity(),"No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }

Here is my app gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "com.strokx.user.stockmanager"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:design:24.2.0'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'com.android.support:support-v4:24.2.0'
    compile 'com.android.support:recyclerview-v7:24.2.0'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.google.code.findbugs:jsr305:2.0.1'
    compile 'com.google.firebase:firebase-database:9.4.0'
    compile 'com.google.firebase:firebase-auth:9.4.0'
    compile 'com.google.firebase:firebase-crash:9.4.0'
    compile 'com.firebase:firebase-client-android:2.4.0'
    compile 'com.journeyapps:zxing-android-embedded:3.3.0@aar'
    compile 'com.google.zxing:core:3.2.1'
    compile 'com.firebaseui:firebase-ui:0.4.3'
    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'  

And Here's my Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.strokx.user.stockmanager">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk tools:overrideLibrary="com.firebase.ui,com.firebase.ui.auth,com.facebook,android.support.customtabs"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/purchase_icon"
        android:label="@string/app_name"
        android:name=".StockManager"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" />
    </application>

</manifest>

Solution

  • Add these dependencies in your build.gradle

    compile 'com.journeyapps:zxing-android-embedded:3.1.0@aar'
    compile 'com.google.zxing:core:3.2.0'
    

    And remove

     compile 'com.google.zxing:core:3.2.1'
    

    Now inside OnClickListener integrate scanner like this way .

      button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
        IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
                    scanIntegrator.setPrompt("Scan a Barcode");
                    scanIntegrator.setBeepEnabled(true);
                    scanIntegrator.setOrientationLocked(true);
                    scanIntegrator.setBarcodeImageEnabled(true);
                    scanIntegrator.initiateScan();
                }
      });
    

    Let me know if it works