Open In App

VideoView in Android

Last Updated : 25 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

VideoView is a UI widget that is used to display video content to the users within android applications. We can add video in this video view from different resources such as a video stored on the user device, or a video from a server. In this article, we will take a look at How to use Video View in the android application. A sample video is given below to get an idea about what we are going to do in this article.

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the code below. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--on below line we are creating a simple text view-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Video View in Android"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!-- adding VideoView to the layout -->
    <VideoView
        android:id="@+id/idVideoView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/idTVHeading"
        android:layout_centerInParent="true" />
      
</RelativeLayout>


Step 3: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.net.Uri
import android.os.Bundle
import android.widget.MediaController
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // on below line we are creating a variable.
    lateinit var videoView: VideoView
    val videoUrl = "Paste Your Video URL Here"
      
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // on below line we are initializing our variables.
        videoView = findViewById(R.id.idVideoView)
  
        // Uri object to refer the
        // resource from the videoUrl
        val uri = Uri.parse(videoUrl)
  
        // sets the resource from the
        // videoUrl to the videoView
        videoView.setVideoURI(uri)
  
        // creating object of
        // media controller class
        val mediaController = MediaController(this)
  
        // sets the anchor view
        // anchor view for the videoView
        mediaController.setAnchorView(videoView)
  
        // sets the media player to the videoView
        mediaController.setMediaPlayer(videoView)
  
        // sets the media controller to the videoView
        videoView.setMediaController(mediaController);
  
        // starts the video
        videoView.start();
  
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
  
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // on below line we are creating variables.
    private VideoView videoView;
     
    // Your Video URL
    String videoUrl = "Paste Your Video URL Here";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // on below line we are initializing our variables.
        videoView = findViewById(R.id.idVideoView);
  
        // Uri object to refer the 
        // resource from the videoUrl
        Uri uri = Uri.parse(videoUrl);
  
        // sets the resource from the
        // videoUrl to the videoView
        videoView.setVideoURI(uri);
  
        // creating object of 
        // media controller class
        MediaController mediaController = new MediaController(this);
  
        // sets the anchor view 
        // anchor view for the videoView
        mediaController.setAnchorView(videoView);
  
        // sets the media player to the videoView
        mediaController.setMediaPlayer(videoView);
  
        // sets the media controller to the videoView
        videoView.setMediaController(mediaController);
  
        // starts the video
        videoView.start();
  
    }
}


Now run your application to see the output of it. 

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads