Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is a SurfaceView created from a layout (programmatically)?

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?

like image 383
Ákos Maróy Avatar asked Apr 29 '26 21:04

Ákos Maróy


1 Answers

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);
like image 183
Jager7 Avatar answered May 02 '26 13:05

Jager7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!