Set Background color of Surface View

Gaurav Arora picture Gaurav Arora · Mar 7, 2013 · Viewed 21.6k times · Source

I want to set a background color to the surface view to my camera surface view.

I am using this for implementing the same. But this example is not complete. Can anyone please help me with some other useful link.

Thanks

Answer

wrkwrk picture wrkwrk · Jan 15, 2015

There's a workaround to do this.

  1. add a parent viewgroup for the surfaceview, set the background color to this viewgroup instead of the surfaceview;

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark">
    
        <com.example.surfacetest.MySurface
            android:id="@+id/surface"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </FrameLayout>
    
  2. add the following for the SurfaceView instance;

    surfaceView.setZOrderOnTop(true);
    surfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    

Now you get the background color you want and the surfaceview is tanslucent.