Reputation: 200
I am very new to mobile development and I trying to make an android app using Xamarin(visual studio). I am trying to integrate zxing barcode scanner into my app and I have found many examples of it's integration into app but they are all using Activity. I want to use fragments instead of Activity. I tried to make my fragment and the scanning screen do come up but it is not scanning anything. Can someone please point into the right direction. Thank you.
Updated file
Here is my Fragment2.cs:
public class Fragment2 : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.MySettingsView, container, false);
ImageButton scanBtn = view.FindViewById<ImageButton>(Resource.Id.btnScan);
TextView results = view.FindViewById<TextView>(Resource.Id.results);
scanBtn.Click += async (sender, e) =>
{
MobileBarcodeScanner.Initialize(Activity.Application);
var scanner = new MobileBarcodeScanner();
var result = await scanner.Scan();
if (result != null)
{
return;
}
Console.WriteLine($"Scanned Barcode: {result}");
Activity.RunOnUiThread(() =>
{
results.Text = result.Text;
});
};
return view;
}
}
Upvotes: 2
Views: 1128
Reputation: 200
@apineda's code work perfectly but I was having the auto-focusing issues. After doing so research, I found a work around to that problem. Here is my scanning fragment.
public class Fragment2 : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.MySettingsView, container, false);
ImageButton scanBtn = view.FindViewById<ImageButton>(Resource.Id.btnScan);
TextView results = view.FindViewById<TextView>(Resource.Id.results);
scanBtn.Click += async (sender, e) =>
{
MobileBarcodeScanner.Initialize(Activity.Application);
var scanner = new MobileBarcodeScanner();
var result = (ZXing.Result)null;
result = await scanner.Scan();
if (result == null)
{
scanner.AutoFocus();
return;
}
Console.WriteLine($"Scanned Barcode: {result}");
Activity.RunOnUiThread(() =>
{
results.Text = result.Text;
});
};
return view;
}
}
Upvotes: 2
Reputation: 9356
I am changing a few things:
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// you don't need this you can safely remove it.
//var ignored = base.OnCreateView (inflater, container, savedInstanceState);
// changed this line to properly inflate the fragment's layout
var view = inflater.Inflate (Resource.Layout.fragment2_layout, container, false);
ImageButton scanBtn = view.FindViewById<ImageButton> (Resource.Id. btnScan);
TextView results = view.FindViewById<TextView> (Resource.Id. Results);
scanBtn.Click += async (sender, e) => {
// you don not create a new instance of the Android Application
// but get the one already created. From an activity you
// just call `Application` but from inside a fragment you need to get
// the fragment's activity then get the Application.
MobileBarcodeScanner.Initialize (Activity.Application);
var scanner = new MobileBarcodeScanner ();
var result = await scanner.Scan ();
// The if was inverted.
if (result == null)
{
return;
}
Console.WriteLine ($"Scanned Barcode: {result}");
// Using this you are sure it will run in the UI thread
// as you will be updating an UI element.
Activity.RunOnUiThread (() => {
results.Text = result.Text;
});
};
return view;
}
Hope this helps!
Upvotes: 2