Android VideoView Using Kotlin With Example

In this article, we will learn about android VideoView using Kotlin. We will go through various example that demonstrates how to use different attributes of VideoView. For example,

In this article, we will get answer to questions like –

  • What is VideoView?
  • Why should we consider VideoView while designing ui for any app?
  • What are possibilities using VideoView while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin VideoView Output Android VideoView Using Kotlin

Tutorialwing Kotlin VideoView Output

Getting Started

We can define android VideoView widget as below –

VideoView is subclass of View that are used to display a video file.

Now, how do we use VideoView in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as VideoView. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android VideoView Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using VideoView in Kotlin

Follow steps below to use VideoView in newly created project –

  • We need to store video to play in locally. So, create a new folder, raw, in res/raw folder. Then, download video given in source code.
    Or, you can also put any video you want to show in videoView. Here, we have named the video sample_video.mp4.
  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">VideoView</string>
        <string name="play">Play</string>
        <string name="pause">Pause</string>
        <string name="paused">Paused…</string>
        <string name="playing">Playing…</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/btnPlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/play"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <VideoView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnPlay" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    In activity_main.xml file, we have defined VideoView and Button widgets. Now, we will access this widgets in kotlin file. Then, we will perform some operations on it.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.videoview
    
    import android.net.Uri
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Toast
    import com.tutorialwing.videoview.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupVideoView()
    	}
    
    	private fun setupVideoView() {
    		val path = "android.resource://" + packageName + "/" + R.raw.sample_video
    		binding.videoView.setVideoURI(Uri.parse(path))
    
    		binding.btnPlay.setOnClickListener {
    			val isPlaying = binding.videoView.isPlaying
    			binding.btnPlay.setText(if (isPlaying) R.string.play else R.string.pause)
    
    			val msg = getString(if (isPlaying) R.string.paused else R.string.playing)
    			Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
    			if (isPlaying) {
    				binding.videoView.pause()
    			} else {
    				binding.videoView.start()
    			}
    		}
    
    		binding.videoView.setOnCompletionListener {
    			binding.btnPlay.text = getString(R.string.play)
    		}
    	}
    }
    

    We have accessed Button and VideoView using kotlin file (i.e. MainActivity.kt). After that, we have accessed the path of video resource stored in res/raw folder. Note that you will have to create raw folder under main/res folder in the application. We store resources such as audio, video etc. in raw folder.

    We have also set click listener in button to play/pause video in the videoView.

Now, run the application. We will get output as below –

Tutorialwing Kotlin VideoView Output Android VideoView Using Kotlin

Tutorialwing Kotlin VideoView Output

Different Attributes of VideoView in XML

Now, we will see how to use different attributes of Android VideoView using Kotlin to customise it –

Set Id of VideoView

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of VideoView using android:id attribute like below –

    <VideoView
        android:id="@+id/videoView_ID"
        />

Here, we have set id of VideoView as videoView_ID using android:id=”” attribute. So, if we need to reference this VideoView, we need to use this id – videoView_ID.
Learn to Set ID of VideoView Dynamically

Set Width of VideoView

We use android:layout_width=”” attribute to set width of VideoView.
We can do it as below –

    <VideoView
        android:id="@+id/videoView_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of VideoView Dynamically

Set Height of VideoView

We use android:layout_height=”” attribute to set height of VideoView.
We can do it as below –

    <VideoView
        android:id="@+id/videoView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of VideoView Dynamically

Set Padding of VideoView

We use android:padding=”” attribute to set padding of VideoView.
We can do it as below –

    <VideoView
        android:id="@+id/videoView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in VideoView using android:padding=”” attribute.
Learn to Set Padding of VideoView Dynamically

Set Margin of VideoView

We use android:layout_margin=”” attribute to set margin of VideoView.
We can do it as below –

    <VideoView
        android:id="@+id/videoView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in VideoView using android:layout_margin=”” attribute.
Learn to Set Margin of VideoView Dynamically

Set Background of VideoView

We use android:background=”” attribute to set background of VideoView.
We can do it as below –

    <VideoView
        android:id="@+id/videoView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in VideoView using android:background=”” attribute.
Learn to Set Background of VideoView Dynamically

Set Visibility of VideoView

We use android:visibility=”” attribute to set visibility of VideoView.
We can do it as below –

    <VideoView
        android:id="@+id/videoView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of VideoView using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of VideoView Dynamically

Till now, we have see how to use android VideoView using Kotlin. We have also gone through different attributes of VideoView to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android VideoView Widget

Below are the various attributes that are used to customise android VideoView Widget. However, you can check the complete list of attributes of VideoView in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of videoView are –

Sr. XML Attributes Description
1 android:clickable Sets whether this view is clickable or not
2 android:elevation Sets elevation of the view
3 android:focusable Sets whether this view should take focus or not
4 android:id Sets id of the view
5 android:longClickable Sets whether this view should respond to long click or not
6 android:onClick Sets what to do when this view is clicked
7 android:padding Sets padding of the view
8 android:paddingBottom Sets bottom padding of the view
9 android:paddingEnd Sets padding to right edge of the view
10 android:paddingHorizontal Set padding to left and right edges of the view
11 android:paddingLeft Sets padding to left edge of the view
12 android:paddingRight Sets padding to right edge of the view
13 android:paddingStart Sets padding to left edge of the view
14 android:paddingTop Sets padding to top edge of the view
15 android:visibility Sets visibility of the view

We have seen different attributes of VideoView and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is VideoView, how can we use android VideoView using Kotlin ? etc. We also went through different attributes of android VideoView.

Leave a Reply

We are actively looking for Paid Content Writer. Email us at tutorialwing@gmail.com if you are interested.