Search code examples
androidandroid-dialogfragment

Video fullscreen mode in dialogFragment


I am using this code to open a fragmentdialog from a custom adapter:

HomeActivity activity = (HomeActivity) (context);
            FragmentManager fm = activity.getSupportFragmentManager();
            editor.putString("post_media", remainder);
            editor.apply();
            PostVideoFragment alertDialog = new PostVideoFragment();
            alertDialog.show(fm, "fragment_alert");

And this is the layout from the fragment dialog:

<RelativeLayout 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:background="@android:color/black">

    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>

And this is the code for the fragmentdialog:

public class PostVideoFragment extends DialogFragment {

    VideoView post_video;



    public static String MISDATOS= "MisDatos";

    SharedPreferences prefs;
    SharedPreferences.Editor editor;
    String post_media;


    public static PostVideoFragment newInstance() {
        PostVideoFragment fragment = new PostVideoFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_post_video, container, false);




        prefs = getActivity().getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);

        editor = prefs.edit();


       post_media = prefs.getString("post_media", "no");

        String url = "https://f..postsmedia/";

        final VideoView videoView;
        videoView = (VideoView)v.findViewById(R.id.video);
        videoView.setVideoPath(url+post_media);
        videoView.start();



        return v;
    }
}

The functionality is right, the video is played, but I would need to play it at full width when the screen is in landscape mode, now it is only shown in a very small mode.

Here is the output:

Portrait mode:

enter image description here

Landscape mode:

enter image description here


Solution

  • In onStart of your DialogFragment, you can set the height and width to MATCH_PARENT like below:

    @Override
    public void onStart()
    {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null)
        {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setLayout(width, height);
            //You need to add below lines in order to show FullScreen VideoView without the status bar
            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }