androidandroid-studioandroid-videoview

Android Studio: Video longer than screen - unable to scroll down?


I have an app that contains a video on loop which is longer than the length of the screen, however it is not possible to scroll down and see the bottom section of the video (located outside the bottom end of the screen).

How can I allow the video to be scrolled up and down?

activity_main.xml:

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

    <VideoView
        android:id="@+id/dig_lic"
        android:layout_width="wrap_content"
        android:layout_height="2950px" />
</LinearLayout>

MainActivity.kt:

class MainActivity: ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle ? ) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val videoView = findViewById < VideoView > (R.id.dig_lic)
    val packageName = "android.resource://" + getPackageName() + "/" + R.raw.dig_lic_video
    val uri = Uri.parse(packageName)
    videoView.setOnPreparedListener {
      it.isLooping = true
    }
    videoView.setVideoURI(uri)
    videoView.start()
  }
}

Solution

  • You are facing this issue is because you have set static height for video view i.e 2950 px. This makes your video view stretched and adding scrollview to if makes it scrollable.

    1. Scrollview Example

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
            <VideoView
                android:id="@+id/dig_lic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    If that doesn't fixes your problem place your VideoView in a ScrollView control.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <VideoView
                android:id="@+id/dig_lic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
        </ScrollView>
    
    </LinearLayout>
    

    Or use a WebView when the format of the video allows it.

    2. Webview Example

    Layout (activity_main.xml):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

    Activity (MainActivity.kt):

    class MainActivity : ComponentActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val webView = findViewById<WebView>(R.id.web_view)
    
            // Load a website using URL
            val url = "your video url .com "
            webView.loadUrl(url)
    
            // Alternatively, load HTML content from a string
            // val htmlString = "<h1>Hello from a String!</h1>"
            // webView.loadData(htmlString, "text/html", "UTF-8")
    
            // Enable JavaScript (optional, but recommended for many websites)
            webView.settings.javaScriptEnabled = true
        }
    }