Android VideoView Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn about android videoView using kotlin in any android application. We will also learn about different attributes of android videoView that can be used to customise this widget.

Output

Tutorialwing Kotlin VideoView Output Android VideoView Using Kotlin

Tutorialwing Kotlin VideoView Output

Getting Started

Android VideoView can be defined as below –

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

Different Attributes of Android VideoView 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



Example of Android VideoView Using Kotlin

At first, we will create android application. Then, we will use videoView using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as VideoView. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use videoView using kotlin in the application.

2. Modify values folder

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>

3. Use VideoView Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

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

	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginBottom="5dp"
		android:layout_marginTop="5dp"
		android:text="@string/play"/>

	<VideoView
		android:id="@+id/videoView"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="5dp"/>

</LinearLayout>

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.

4. Access VideoView Widget in Kotlin file

Open src/main/java/com.tutorialwing.videoview/MainActivity.kt file and add below code into it.

package com.tutorialwing.videoview

import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.Toast
import android.widget.VideoView

class MainActivity : AppCompatActivity() {

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

        val videoView = findViewById<VideoView>(R.id.videoView)
        val path = "android.resource://" + packageName + "/" + R.raw.a
        videoView?.setVideoURI(Uri.parse(path))

        val button = findViewById<Button>(R.id.button)
        button?.setOnClickListener({
            val isPlaying = videoView.isPlaying
            button.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) {
                videoView.pause()
            } else {
                videoView.start()
            }
        })
    }
}

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.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.videoview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Android videoView using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners