3

I wonder when a SurfaceView is actually created, from a layout?

this is in a scenario where a layout is created from a Service for a LiveCard on Google Glass - thus there is no activity for which this layout can be set using setContentView().

still, I wonder what does it take for a SurfaceView to get created programmatically

I have the following layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <SurfaceView
        android:id="@+id/camera_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

and then I have:

    mLayout = (FrameLayout) inflater.inflate(R.layout.orientation_recorder, null);
    mLayout.setWillNotDraw(false);

    cameraView = (SurfaceView) mLayout.findViewById(R.id.camera_view);
    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                Log.e("OR", "camera view sufrace destroyed");
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Log.e("OR", "camera view sufrace created");
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                    int height) {
                Log.e("OR", "camera view sufrace changed "
                        + " format: " + format
                        + " width: " + width
                        + " height: " + height);
            }
    });

but, I can't get the SurfaceView to get created, e.g. surfaceCreated() is never called above.

what would be needed to make this happen?

Ákos Maróy
  • 919
  • 2
  • 10
  • 19

1 Answers1

4

You have to set the content view to your Layout and add the surface view to the Layout in your activity or main activity class

setContentView(R.layout.main);<---- basically points to and uses your xml

LinearLayout layout = (LinearLayout)findViewById(R.id.layout);<---- points to Layout defined in xml

layout.addView(new SurfaceView(this));<----adds SurfaceView to layout

You can do that or this call in you main activity

view surface = new SurfaceView(this);
SetContentView(surface);
Túlio Calazans
  • 179
  • 2
  • 15
Jager7
  • 51
  • 5