How to add VideoView in Alert Dialog in Android

In this android programming source code example, we are going to add VideoView in Alert Dialog in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to add VideoView in Alert Dialog in Android.

activity_main.xml

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


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_centerInParent="true"
        android:text="Show AlertDialog"
        android:onClick="showAlert"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:textSize="15sp"/>

</RelativeLayout>

MainActivity.java

import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;

public class VideoViewActivity8 extends AppCompatActivity {

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

    }

    public void showAlert(View view){
        final VideoView taskvideoview = new VideoView(this);
        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.test);
        taskvideoview.setVideoURI(uri);
        taskvideoview.start();
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Your Title");
        alertDialogBuilder.setMessage("What do you want?");
        alertDialogBuilder.setView(taskvideoview);
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment