1

Is there a way to center a VideoView in activity on portrait. I tried many options, for example:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent" >

<VideoView
    android:id="@+id/videoView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center|center_vertical" />
</FrameLayout>

Here is how I start the activity:

    val intent = Intent(this, VideoActivity::class.java)
    startActivity(intent)

Here is the code in the activity:

class VideoActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_video)

    val uriPath = "android.resource://" + packageName + "/raw/ad"
    val uri = Uri.parse(uriPath)

    videoView.setVideoURI(uri)
    videoView.requestFocus()
    videoView.start()

   }

}

It keeps aligning to the top: enter image description here

bashan
  • 3,572
  • 6
  • 41
  • 58

2 Answers2

6

You can use this below code.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Its work perfectly.

Shohel Rana
  • 2,332
  • 19
  • 24
2

Try this,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>
Bhavesh Desai
  • 753
  • 4
  • 20