Skip to content
Related Articles

Related Articles

Improve Article
VideoView in Kotlin
  • Difficulty Level : Medium
  • Last Updated : 18 Feb, 2021

VideoView class of Kotlin is used to display video files in the android application. This class supports the 3gp and MP4 video formats. VideoView class is capable of playing a video file either from local storage, specific URL or from a resource file. The drawback of this class is that it does not retain the full state of the video file if the application goes into the background it means that the current play position, play state, or any kind of subtitle track can not be restored.

The class hierarchy of VideoView class in Kotlin

Diagram of Class hierarchy of VideoView class in Kotlin

XML attributes of VideoView widget

XML attributeDescription
android:idUse to uniquely identify a VideoView
android:layout_widthTo set width of the VideoView
android:layout_heightTo set height of the VideoView
android:layout_marginTo fix the margin from top, bottom, start and end
app:layout_constraintTo fix the position in an activity


This example demonstrates steps involved in adding a video file in an android activity from local storage. A media controller is also added to the activity to control the play and pause the position of the video.

Note: Following steps are performed on Android Studio version 4.0

Create new project

  1. Click on File, then New => New Project.
  2. Select language as Kotlin.
  3. Select the minimum SDK as per your need.

Add VideoView in activity_main.xml file

Below is the code for activity_main.xml file to add a TextView and a VideoView in an activty.

<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintVertical_bias="0.01999998" />
        app:layout_constraintTop_toBottomOf="@+id/textView" />

Open MainActivity.kt file

Below is the code for MainActivity.kt file to access VideoView widget in Kotlin file and to add a media controller for it.

Note: Make sure to create a directory named raw in the resource file of your project and add the video file in that directory using file explorer.

package com.example.videoviewinkotlin
import android.os.Bundle
import android.view.View
import android.widget.MediaController
import android.widget.Toast
import android.widget.VideoView
class MainActivity :  AppCompatActivity() {
    // declaring a null variable for VideoView
    var simpleVideoView: VideoView? = null
    // declaring a null variable for MediaController
    var mediaControls: MediaController? = null
    override fun onCreate(savedInstanceState: Bundle?){
        // assigning id of VideoView from
        // activity_main.xml layout file
        simpleVideoView = findViewById<View>( as VideoView
        if (mediaControls == null) {
            // creating an object of media controller class
            mediaControls = MediaController(this)
            // set the anchor view for the video view
        // set the media controller for video view
        // set the absolute path of the video file which is going to be played
                                       + packageName + "/" + R.raw.gfgvideo))
        // starting the video
        // display a toast message
        // after the video is completed
        simpleVideoView!!.setOnCompletionListener {
            Toast.makeText(applicationContext, "Video completed",
        // display a toast message if any
        // error occurs while playing the video
        simpleVideoView!!.setOnErrorListener { mp, what, extra ->
            Toast.makeText(applicationContext, "An Error Occured " +
                    "While Playing Video !!!", Toast.LENGTH_LONG).show()

Modify strings.xml file

All the strings which are used in the activity are listed in this file.

    <string name="app_name">VideoView in Kotlin</string>
    <string name="heading_of_activity">Running a video file in an activity</string>

Open AndroidManifest.xml file

Below is the code for AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://"
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            android:resource="@array/preloaded_fonts" />

Run as emulator

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

My Personal Notes arrow_drop_up
Recommended Articles
Page :