14

I'm using ViewPager for swiping between my Fragments. Is it possible to integrate a QR scanner (zxing or any other) inside of a Fragment so that I can swipe from scanners View to another Fragment and vice versa?

As far as I can tell, the majority of qr scanners require me to use an extended Activity class and to start it for result, handling it in onActivityResult, which is not an option to me

Nikolay Arhipov
  • 147
  • 3
  • 6
  • A quick search would reveal what you're looking for: http://stackoverflow.com/questions/4782543/integration-zxing-library-directly-into-my-android-application – Michał Klimczak Jul 19 '12 at 10:26
  • 1
    That's not what I've been asking for, I know how to integrate zxing, the problem is that to use it I have to start separate Activity, but I need to use it in a Fragment – Nikolay Arhipov Jul 19 '12 at 10:30
  • It's exactly what you're looking for. The accepted answer tells you how to directly integrate 'zxing' into your code instead of using `Intent` based approach which, as you say, is not an option. Then you can use it in your `Fragment`, `Activity`, whatever. Plus, `Intent` based (using `onActivityResult`) option is also possible to do in `Fragment`. You can use results from `onActivityResult` in your Fragment, after all, every `Fragment` needs a parent `Activity`. – Michał Klimczak Jul 19 '12 at 10:37
  • 2
    Curious if you ever found an answer on this? I have checked that link and read over the accepted answer, and all it explains is how to integrate ZXing with your project; it does not even HINT as to how to specifically integrate the scanner into a custom fragment. Unless I'm missing something obvious here? I've spent hours trying to get it to work within a fragment with no luck, using IntentIntegrator, etc. It will only show in full-screen mode or in an extended Activity. Any help on this would be greatly appreciated! – svguerin3 May 10 '13 at 05:38
  • @svguerin3 : Did you find any library that does that for you? I been searching and trying different things as you did for last few days but no success till now. – Shobhit Puri Jun 14 '13 at 19:56
  • I have answered it here, please have a look http://stackoverflow.com/a/35822289/3470479 – Prakhar Kulshreshtha Mar 06 '16 at 02:21

1 Answers1

5

The barcodefragmentlib is the one you are looking for. And its wiki shows you how to integrate it (although I used a different way before the wiki comes out).

However after fully integrated it and solve all the issues for our commercial app, I feel like put a QR scanner inside a fragment and switch using ViewPager is not a good idea. Some of the known issues with this idea and the mentioned library to me are:

  • Frequently switch between the QR fragment and other fragments will make your app laggy and easy to crash;
  • Fragment replacement is not so easy to handle as Activity, QR fragment is even worse;
  • Handling portrait/horizontal scan mode is not available by default (related to original ZXing lib)
  • When first open the QR fragment, it may black out your screen for a very short time
  • Scanner is not working well on Galaxy S4 or other new devices with higher resolution camera/display

Although these issues mentioned above can be solved with certain effort, it is really a trade off.

Felixqk
  • 1,624
  • 1
  • 11
  • 6