Search code examples
androidandroid-layoutandroid-videoview

Center video at center of screen - Videoview Android


I have a video view, that plays a certain video this is his xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">



Some other views


    <VideoView
        android:id = "@+id/video"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_gravity="center"
        android:layout_alignParentStart="true"
        />



</RelativeLayout>

I am trying to center the video in it, yet when ever I play the video, It does not appear at the center, and the location changes a bit depends on the video, how do you center it?


Solution

  • As @jaydroider told, you could use layout_centerInParent=true

    In this example, i changed width and height and changed background color of main layout.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@android:color/white">
    
        <VideoView
            android:id="@+id/video"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:layout_gravity="center" />
    
    </RelativeLayout>
    

    Don't use android:layout_alignParentTop="true" and android:layout_alignParentStart="true" because if you use it, layout always showing at TOP/LEFT position