Search code examples
androidandroid-videoviewbottomsheetdialogfragment

While using VideoView in BottomSheetDialogFragment, Video is loading always at the top


I want to load video in VideoView which is in BottomSheetDialogFragment of having 140dp height, but when I open dialog then video is playing at the top of screen

BottomSheetDialogFragment:

XML code:

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

<VideoView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="140dp" />

<TextView
    android:id="@+id/txt_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:layout_gravity="center"
    android:layout_marginTop="23dp"
    android:layout_marginLeft="19dp"
    android:layout_marginRight="19dp"
    android:textColor="@color/black"
    android:letterSpacing="0.01"
    android:text="dummy_large_text"
    android:lineSpacingExtra="6.5sp"
    tools:ignore="RtlSymmetry"
    tools:targetApi="lollipop" />

<Button
    android:id="@+id/btn_back"
    android:layout_width="match_parent"
    android:layout_height="60.2dp"
    android:textColor="@color/white"
    android:layout_marginTop="21.5dp"
    android:text="@string/back"
    android:background="@color/colorPrimary"
    android:letterSpacing="0.09"
    tools:targetApi="lollipop" />

 </LinearLayout>

Java Code:

holder.videoView.setVideoURI(Uri.parse(imgPathFile + File.separator + dataModels.get(position).getFileName()));
holder.videoView.setMediaController(new MediaController(context)); //sets MediaController in the video view
holder.videoView.requestFocus();//give focus to a specific view
holder.videoView.start();//starts the video

This is how it looks now


Solution

  • As suggested by my mentor I resolved this issue by using external library universalvideoview from GitHub.

    If someone wants to add VideoView in their project then use below library

    https://github.com/linsea/UniversalVideoView