Android VideoView Tutorial 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 how to use android videoView widget in any android application. We will also go through different attributes of videoView widget that can be used to customise it.

Output

Tutorialwing Android VideoView Output Android VideoView Tutorial With example

Tutorialwing Android VideoView Output

Getting Started

VideoView widget can be defined as below –

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

Attributes of Android VideoView Widget

Some of the popular attributes of videoView are –

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



Example of Android VideoView Widget

At first, we will create android application. Then, we will use VideoView widget in this application.

1. Creating New Project

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as VideoView. Then, 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. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use android videoView widget 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 res/layout/activity_main.xml file. Then, 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 these widgets in java file in the application.

4. Access VideoView Widget in java file

Open src/main/java/com.tutorialwing.videoview/MainActivity.java file. Then, 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.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final VideoView videoView = findViewById(R.id.videoView);
		if (videoView != null) {
			String path = "android.resource://" + getPackageName() + "/" + R.raw.a;
			videoView.setVideoURI(Uri.parse(path));
		}

		final Button button = findViewById(R.id.button);
		if (button != null) {
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (videoView != null) {
						boolean isPlaying = videoView.isPlaying();
						button.setText(isPlaying ? R.string.play : R.string.pause);

						String msg = getString(isPlaying ? R.string.paused : R.string.playing);
						Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
						if (isPlaying) {
							videoView.pause();
						} else {
							videoView.start();
						}
					}
				}
			});
		}
	}
}

In MainActivity.java file, we have accessed android videoView widget. Then, we accessed video resource that was stored in res/raw folder. After that, we have set video resource to videoView. At last, we set click listener to button that play/pause video in videoView. If video is already playing, we pause the video. otherwise, we play the video when button is clicked. Method videoView.start() is being called to play the video. Method videoView.pause() is being called to pause the video.

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

AndroidManifest.xml

Code inside src/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 tutorial on Android VideoView widget.

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