Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
How to Implement a Video Player Inside an AlertDialog in Android?
Next article icon

How to Implement a Video Player Inside an AlertDialog in Android?

Last Updated : 04 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Mainly alert dialogues are used to display an important message to the user. It is possible to embed a video player inside it. This allows developers to create dynamic and interactive video-based dialogs that are useful in various use cases. In this article, we will explore the step-by-step process of implementing a video player inside an AlertDialog in Android. A sample video is given below to get an idea about what we are going to do in this article.

Step By Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio and select the language as Kotlin.

Step 2: Change the StatusBar Color

Navigate to app > res > values > themes > themes.xml and add the below code under the style section in the themes.xml file.

<item name="android:statusBarColor" tools:targetApi="l">#308d46</item>

Step 3: Creating a layout for Alert Dialog

Navigate to the app > res > layout > create a file named dialog_video_player.xml and add the below code to that file. Below is the code for the dialog_layout.xml file.  It represents the UI of our Alert Dialog, It contains the Video View to play the video, TextView, and a Close button to dismiss the alert dialog.

dialog_video_player.xml:

XML
<!-- Layout for the video player dialog --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">      <VideoView         android:id="@+id/videoView"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </RelativeLayout> 

Step 4: Working with activity_main.xml

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. This xml file represents our app UI, our UI contains one button by clicking it the Alert dialog is opened.

activity_main.xml:

XML
<!-- layout for the main activity --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="center"     android:orientation="vertical"     tools:context=".MainActivity">     <Button         android:id="@+id/vdo_dialog"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Open Video Dialog"/>  </LinearLayout> 

Step 5: Working with the MainActivity.kt file

Go to the MainActivity.kt and follow the below code. Below is the code for the MainActivity.kt. Comments are added inside the code to understand the code in more detail. This activity contains the main logic to create an Alert dialog and add a Video Player inside the Alert Dialog.

MainActivity.kt:

Kotlin
package com.example.gfg  import android.app.AlertDialog import android.content.Context import android.net.Uri import android.os.Bundle import android.view.LayoutInflater import android.widget.Button import android.widget.MediaController import android.widget.VideoView import androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)         // Get the path for the sample video         val videoUri = Uri.parse("android.resource://" + packageName + "/" + R.raw.sample_video)         // Find the video dialog button and set the click listener          var btn_video_dialog = findViewById<Button>(R.id.vdo_dialog)         btn_video_dialog.setOnClickListener {             showVideoPlayerAlertDialog(this, videoUri)         }     }      // Function to show the video player dialog     private fun showVideoPlayerAlertDialog(context: Context, videoUri: Uri) {         // Inflate the dialog layout         val dialogLayout = LayoutInflater.from(context).inflate(R.layout.dialog_video_player, null)         // Find the VideoView in the dialog layout         val videoView = dialogLayout.findViewById<VideoView>(R.id.videoView)         // Set the video URI to the VideoView         videoView.setVideoURI(videoUri)         // Create a MediaController and set it as            // the anchor view for the VideoView         val mediaController = MediaController(context)         mediaController.setAnchorView(videoView)         videoView.setMediaController(mediaController)         // Start playing the video         videoView.start()         // Create an AlertDialog builder         val builder = AlertDialog.Builder(context)             .setTitle("Video Player Dialog")             .setView(dialogLayout)             .setPositiveButton("Close") { dialog, _ -> dialog.dismiss() }         // Create and show the dialog         val dialog = builder.create()         dialog.show()     } } 

Output:


Next Article
How to Implement a Video Player Inside an AlertDialog in Android?

C

chinmaya121221
Improve
Article Tags :
  • Kotlin
  • Android

Similar Reads

    How to Add an Icon Inside an AlertDialog in Android?
    In this tutorial, you will learn how to display an Icon inside a custom AlertDialog in an Android app using Kotlin. While the default AlertDialog offers basic styling, customizing its layout allows you to add icons, colors, and other UI components to enhance the user experience.Step By Step Implemen
    3 min read
    How to Implement Loading AlertDialog in Android?
    AlertDialog is defined as the small window that shows a particular message to the user when the user performs or commits certain action. In this article, we are going to build a simple android application in which we learn how to implement a Loading AlertDialog that means whenever the user clicks on
    6 min read
    Display AlertDialog for Latest Update of an Android Application
    While using your smartphones you must have gone through a situation where an app asks the user to update it to use it further. Here, we are going to implement the same and see how an app asks users to update it by showing an update alert dialog. What we are going to build in this application? Here i
    3 min read
    How to Implement View Binding Inside Dialog in Android?
    In android, View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views th
    4 min read
    Complete guide on How to build a Video Player in Android
    This article explains the stepwise process as to how to build a Video Player using Android Studio. For viewing videos in android, there is a special class called "Exoplayer". In this article, we will be having 2 videos which are connected by the "Dialog box", i.e. a dialog box will come after comple
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences