0

In my Xamarin.Android app, I want to use ZXing to scan barcode. I want to display the scanner in the view of an activity.

Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="5">
    <Button
        android:text="Scan with Default Overlay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/buttonScanDefaultView"
        android:layout_weight="1" />
  <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scanView"
        android:layout_weight="2" />
</LinearLayout>
protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    scannerFragment = new ZXingScannerFragment ();
    scannerFragment.CustomOverlayView = CustomOverlayView;
    scannerFragment.UseCustomOverlayView = UseCustomOverlayView;
    scannerFragment.TopText = TopText;
    scannerFragment.BottomText = BottomText;
    this.FragmentManager.BeginTransaction ()
                .Replace (Resource.Id.scanView, scannerFragment, "ZXINGFRAGMENT")
                .Commit ();
}

I'm getting an error stating that I cannot convert support.v4.fragment into android.app.Fragment.

Can anyone advise what I'm doing wrong and how should I approach this to get the scanner view (of ZXing) in a layout of my current activity.

Alex Sorokoletov
  • 3,102
  • 2
  • 30
  • 52
TheDeveloper
  • 1,127
  • 1
  • 18
  • 55

1 Answers1

1

ZXingScannerFragment derives from Android.Support.V4.App.Fragment while the Activity.FragmentManager expects fragments derived from Android.App.Fragment.

Now, how to fix that:

  1. Inherit your activity from any activity that works with Android.Support.V4. Easiest would to use Android.Support.V4.App.FragmentActivity from package Xamarin.Android.Support.v4 that is already installed by ZXing.Net.Mobile package as a dependency.

  2. When you have correct activity, you can use this.SupportFragmentManager instead of this.FragmentManager to work with Support.V4-based fragments.

So, layout you have is good. Code should be updated to something like:

using Android.App;
using Android.Widget;
using Android.OS;
using ZXing.Mobile;
using Android.Support.V4.App;

namespace ZXingSample
{
    [Activity(Label = "ZXing Sample", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : FragmentActivity
    {
        int count = 1;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);
            var scannerFragment = new ZXingScannerFragment();
            scannerFragment.UseCustomOverlayView = false;
            scannerFragment.TopText = "Scan your code";
            scannerFragment.BottomText = "Then proceed";
            this.SupportFragmentManager.BeginTransaction()
                        .Replace(Resource.Id.scanView, scannerFragment, "ZXINGFRAGMENT")
                        .Commit();
        }
    }
}

Launching the app, you will see your scanner:

sample app

Alex Sorokoletov
  • 3,102
  • 2
  • 30
  • 52
  • Its working now, but is it possible to call v4.fragment that app. fragment ? – TheDeveloper Mar 27 '17 at 15:04
  • If I understand you correctly, all you need is use fragment without any options like custom overlay, top/bottom texts. – Alex Sorokoletov Mar 27 '17 at 15:08
  • Its working now Alex, one other question, is it possible to call v4.fragment from app. fragment ? – TheDeveloper Mar 27 '17 at 15:29
  • Check this answer, @TheDeveloper http://stackoverflow.com/a/6672688/883738 – Alex Sorokoletov Mar 27 '17 at 19:10
  • Its taking about fragments within fragments, but my issues is support.v4.fragment within app.fragment which cannot be solved with their suggestions – TheDeveloper Mar 27 '17 at 19:53
  • @TheDeveloper here is the thing, ff you want to make your app use fragments, and want to target devices before API 11, you must use android.support.v4.app.Fragment. However, if you're only targeting devices running API 11 or above, you can use android.app.Fragment. – Alex Sorokoletov Mar 27 '17 at 23:02
  • thank you for the input, I'm forced t use support.v4.fragment as its a third party api. one more question, now I am getting the custom overlay n all which you shared in the screenshot but the background is black even on the device. I'm not able to scan anything it totally black just as displayed on your screenshot. any advice on that? – TheDeveloper Mar 28 '17 at 00:48
  • @TheDeveloper check camera permissions and read ZXing getting started carefully. My guess it is camera permissions missing. – Alex Sorokoletov Mar 28 '17 at 07:18