In Android 2.3 application I used to support two cameras: one built-in and onу externally MJPG-streamed using Wi-Fi. The built-in camera must be recording video all the time and a user must have the ability to switch view between those. So, each camera had a dedicated SurfaceView to draw on. As the last child added to a FrameLayout has a higher Z-order, I programatically add the required camera view to a FrameLayout last to make it visible. I can’t skip adding built-in camera since it can’t record video without the preview display.
This scheme no longer works with Android 4: the built-in camera preview is always shown in spite of being added first or last. I played with ‘setVisibility’ methods on views and noticed that if one of views is set to be invisible (or gone) then none of them is showed at all. ViewGroup’s ‘bringChildToFront’ method had no effect as well.
So, is there some workaround to make this work on Android 4? And I know that having multiple SurfaceViews is considered bad. Some code follows.
FrameLayout:
<FrameLayout android:id="@id/data"
android:layout_width="fill_parent" android:layout_height="fill_parent"/>
Code that populates the layout (no longer works on Android 4):
private void setCorrectView() {
data.removeAllViews();
List<Recorder> others = recorders.getOthers();
for (Recorder other : others) {
addToTheView(other);
}
addToTheView(recorders.getCurrent());
}
private void addToTheView(Recorder recorder) {
View view = recorder.getView(this); // get recorder's surface view
data.addView(view);
}
The same effect if I use FrameLayouts from XML:
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="@id/data"
android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<FrameLayout
android:id="@+id/data_invisible"
android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</FrameLayout>
Wherever I put built-in camera’s surface view – it’s always shown. Looks like internal camera’s preview is now always on top in Android 4.
Answering to myself. To make this work I set the views’ visibility to View.VISIBLE before starting the camera and set it to View.INVISIBLE after. This link may be helpful.