2

My title is maybe a joke for those who know well the subject but I prefer ask you to learn.

So my goal is to display ONE specific fragment in an external display when I connect the device. Obviously I found the nice projet cwac-presentation from commonsware : https://github.com/commonsguy/cwac-presentation

I managed to run the demo of SimpleMirrorActivity and WebMirrorActivity which display respectively a mirror of an EditText and a webpage on the same layout.

Then, I looked at how the class MirrorPresentationActivity works and managed to run it. It displays a webpage in the external display.

But my goal is to display one specific fragment and I don't think there is an example of that. I read lots of time the description of MirrorPresentationFragment but didn't still understand how to run it.

Is it possible to get one example or more explanations to do it?

Thanks, that will help me a lot.

azn0viet
  • 140
  • 1
  • 1
  • 9
  • Do you have code to show? What doesn't work the way you tried it? Any specific errors? – Janis F Dec 08 '14 at 14:26
  • Thanks for you answer, however my code is not good at all because I am just trying to understand how it works and how to run a sample. I am going to try more with the answer of CommonsWare and come back when I will have a correct code ! Thanks again. – azn0viet Dec 08 '14 at 15:53

1 Answers1

1

Step #1: Convert your "one specific fragment" to be a subclass of MirroringFragment. Quoting the documentation:

MirroringFragment works much like a regular Fragment. However, instead of overriding onCreateView(), you override onCreateMirroredContent(). onCreateMirroredContent() takes the same parameters as does onCreateView(), and your job is the same: create the content to be displayed by the fragment. The difference is that your returned View will be wrapped in a MirroringFrameLayout.

Use MirroringWebViewFragment as an example of what this should look like. Or, just use MirroringWebViewFragment directly, if your "one specific fragment" happens to be a WebViewFragment.

Step #2: Create and use a MirrorPresentationFragment. Quoting the documentation:

MirrorPresentationFragment is a PresentationFragment designed to mirror the contents of a MirroringFragment. To use this, create an instance using the newInstance() factory method, taking a Context and the desired Display as parameters. Then, call setMirror() on your MirroringFragment, supplying the MirrorPresentationFragment. From there, you can show() and dismiss() the MirrorPresentationFragment as you would any other PresentationFragment. By having the MirroringFragment on the main screen, and having the MirrorPresentationFragment on an external display, whatever the user manipulates on the screen is rendered to the external display, ideal for presentation settings (e.g., conferences).

For instructions on how to use a PresentationFragment in general (of which MirrorPresentationFragment is a subclass), see the documentation. MirrorPresentationActivity demonstrates using a MirrorPresentationFragment.

Step #3: Beer.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can I just skip to #3? – Janis F Dec 08 '14 at 15:55
  • Thanks for your fast answer and for your project ! I didn't understand the first step so I am going to try again. Thanks :) – azn0viet Dec 08 '14 at 15:57
  • @azn0viet: It is entirely possible that you will find that your use cases do not line up with what the library has to offer. If so, file a feature request on the project issue tracker. – CommonsWare Dec 08 '14 at 16:01
  • I hope it won't be the case, my goal seems to be so easy but finally not ! However, I finally managed to run your example as a fragment and it is already a big step. Thanks, I will continue tomorrow and will let you know if/when I will be able to do what I want :) – azn0viet Dec 08 '14 at 16:45
  • After one day, I managed to do what I want, however it is still complicated for me and that's why I will continue to learn how it works by more examples! Thanks for your help :) – azn0viet Dec 09 '14 at 16:10
  • @CommonsWare Hello and happy new year to you! Concerning to my project, I managed to almost do what I want, that's to say display a mirror of a fragment on an external screen. However, the size of the fragment on the main screen changes and takes the same dimension as the duplicated (the screen size). Is is not disturbing for you? Or is it better like this? Otherwise, is it possible (in the library) to adapt the mirroring size rather than the main fragment? Thanks a lot! – azn0viet Jan 13 '15 at 13:07
  • @azn0viet: "However, the size of the fragment on the main screen changes and takes the same dimension as the duplicated (the screen size)" -- no, it should be adjusting to be the same aspect ratio, IIRC. "Is is not disturbing for you?" -- not especially. "Otherwise, is it possible (in the library) to adapt the mirroring size rather than the main fragment?" -- not in my library at present. My library optimizes for maximum quality of the `Presentation`, as more people are viewing that than are seeing the phone or tablet, in many cases. – CommonsWare Jan 13 '15 at 13:19
  • @CommonsWare Thanks for your quick answer. I understand what you mean, there are some black bands on both sides if we keep the same dimensions as the main fragment. If it was so disturbing for me, it's because I use a ScrollView and a part of the top of my main fragment disappears. So it comes from my code and I will look in depth why it does it. Thanks again for you library :) – azn0viet Jan 13 '15 at 14:30
  • @CommonsWare Hello ! I am now at my last step (hopefully) and I would like not modifying the ratio of my initial fragment and let the black bands. So I think I have to modify the sources and I know your library is under the license Apache 2.0 and will do the necessary. So, I understood it is in the class "AspectLockedFrameLayout.java" and in the method "onMeasure". If "lockedWidth" and "lockedHeight" don't change, it does what I want. But the app crashes when I run the app and then, connect an external display. Do you know how can I do? Thanks a lot (and it works well with a chromecast too) ! – azn0viet Jan 30 '15 at 14:51
  • @azn0viet: "But the app crashes when I run the app and then, connect an external display" -- I cannot really help a lot with that, without knowing what changed and what the crash is. Consider opening a fresh Stack Overflow question. – CommonsWare Jan 30 '15 at 22:14