Search code examples
androidlayoutsurfaceview

set SurfaceView's margin programmatically


I am adding the "SurfaceView" for camera in my layout xml file as,

<SurfaceView
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="95dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>

But I want to set the marginTop dynamically. If I want to set the SurfaceView's marginTop programmatically, How can I do this?

I have searched in the internet. But I didn't get any answer. So anyone help me to do this.


Solution

  • You might do something like:

    int margin = getResources().getDimensionPixelSize(R.dimen.surface_view_margin);
    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    lp.setMargins(margin, 0, 0, 0);
    surfaceView.setLayoutParams(lp);
    

    Does this help?