Position Video Inside a VideoView Position Video Inside a VideoView android android

Position Video Inside a VideoView


Try using a FrameLayout instead. I'm not sure why, but if I use a Linear or Relative in my code it won't center, but FrameLayout does. Here is the XML that fit my video to the screen, preserving the ratio and centering it:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/bg">    <!-- Video player -->    <VideoView        android:id="@+id/surface_view"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_gravity="center"/></FrameLayout>


In order to center the video in the RelativeLayout I added both layout_gravity="center" ad layout_centerInParent="true". It works on my Android 4.3 phone.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <VideoView android:id="@+id/surface_view"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="center"        android:layout_centerInParent="true" /></RelativeLayout>


Cameron's Answer in a programmatic way(in case someone like me needs it) This code is inside onCreate of an activity in my code( 'this' below refers to the activity)

    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);    FrameLayout fl = new FrameLayout(this);    fl.setLayoutParams(lp);    VideoView vv = new VideoView(this);    FrameLayout.LayoutParams lp2 = new FrameLayout.LayoutParams(lp);    lp2.gravity = Gravity.CENTER;    vv.setLayoutParams(lp2);    fl.addView(vv);    setContentView(fl);