How to make VideoView full screen when i click a button?

Viewed 173

I have a VideoView which use to load video from url. I want it have a status bar like youTube. I used MediaController but it just has rewind, forward , pause and resume. I want it have a button which is clicked to full screen and exit full screen. Any help that i appreciate it.

1 Answers

to add full screen button to your video player : 1- Setup the Manifest

<activity android:name=".YourPlayerActivity"
  android:configChanges="keyboardHidden|orientation|screenSize">

** this will prevent the player to reset when change to full screen

2-step 2 : Add a new controller to your xml design ** an imageView for Full screen

3- init FullScreen Button

boolean fullscreen = false;
fullscreenButton = playerView.findViewById(R.id.exo_fullscreen_icon);
fullscreenButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(fullscreen) {
            fullscreenButton.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_fullscreen_open));
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            if(getSupportActionBar() != null){
                getSupportActionBar().show();
            }
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) playerView.getLayoutParams();
            params.width = params.MATCH_PARENT;
            params.height = (int) ( 200 * getApplicationContext().getResources().getDisplayMetrics().density);
            playerView.setLayoutParams(params);
            fullscreen = false;
        }else{
            fullscreenButton.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_fullscreen_close));
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
                                                            |View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                                                            |View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
            if(getSupportActionBar() != null){
                getSupportActionBar().hide();
            }
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) playerView.getLayoutParams();
            params.width = params.MATCH_PARENT;
            params.height = params.MATCH_PARENT;
            playerView.setLayoutParams(params);
            fullscreen = true;
        }
    }
});
Related