31

i have a videoview in my application. the code is like this.

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/opsbuds"
    android:orientation="vertical">

    <TextView
        android:id="@+id/adtxt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"></TextView>

    <VideoView
        android:id="@+id/videoView11"
        android:layout_width="300dip"
        android:layout_height="250dip"
        android:layout_marginLeft="30dip"></VideoView>

    <LinearLayout
        android:id="@+id/llv11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"></LinearLayout>

    <Button
        android:id="@+id/button1211"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/button"
        android:text=" Continue "
        android:textColor="#800080"
        android:textSize="20dp"
        android:textStyle="bold"></Button>
</LinearLayout>
</ScrollView>

the video view width and hieght is mentioned in xml file. What i want is , once i press a button the videoview should come on full screen and once i press back button the videoview should go back to its mentioned size. please help?

Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30
Cool Compiler
  • 857
  • 2
  • 9
  • 20
  • you can only set a video_view full screen by making it a child of relative layout and aligning it parentTop,bottom,right,left (true). This post will help you. http://blog.kasenlam.com/2012/02/android-how-to-stretch-video-to-fill.html – Taimur Hassan Jan 22 '16 at 11:05
  • Possible duplicate of [Android-Video View in Fullscreen](https://stackoverflow.com/questions/3776254/android-video-view-in-fullscreen) – bummi Aug 27 '17 at 11:29

13 Answers13

49

I had to make my VideoView sit in a RelativeLayout in order to make the chosen answer work.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <VideoView android:id="@+id/videoViewRelative"
         android:layout_alignParentTop="true"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
    </VideoView>
    
</RelativeLayout>

As given here: Android - How to stretch video to fill VideoView area Toggling between screen sizes would be as simple as changing the layout parameters as given in the chosen answer.

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
Srikanth
  • 2,014
  • 19
  • 22
  • 2
    Yes. It is true. Only RelativeLayout work for make it full screen. Thanks for the highlight. – Azizi Musa Mar 18 '16 at 04:30
  • 2
    Thanks a lot, by the way since it's 2018, use `"match_parent"` instead, because `"fill_parent"` is deprecated nowadays. – Simple Oct 12 '18 at 17:45
  • Thanks a lot, by the way since it's 2020, use "layout_alignParentStart" and "layout_alignParentEnd" instead, because "layout_alignParentLeft" and "layout_alignParentRight" are deprecated nowadays. – Luis Aguilar Dec 28 '20 at 23:40
41

Set full screen this way,

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
videoView.setLayoutParams(params);

And back to original size, this way.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = (int)(300*metrics.density);
params.height = (int)(250*metrics.density);
params.leftMargin = 30;
videoView.setLayoutParams(params);
Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
Aqif Hamid
  • 3,511
  • 4
  • 25
  • 38
  • 8
    this is not working for me... my video view is in a fragment and nothing happens after setting layout params (params are correct) – shift66 Jun 07 '13 at 08:21
  • 2
    it's not working for me. I think it did not stretch videoview. – Leo Nguyen Oct 22 '13 at 03:47
  • Hi, this works but it always rotates the video in the same direction (left). Is it possible to rotate it manually depending on which way the user wants to turn it? – Androidicus Sep 01 '15 at 12:02
  • @Androidicus how will i set to matchparent when my videoview is inside relativelayout – Erum Sep 16 '15 at 12:23
  • @Erum To set your videoview's width and height to match_parent you can call: videoview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); Is this what you want? Of course for the videoview to fill the screen the parent layout must fill the screen as well. – Androidicus Sep 16 '15 at 12:29
  • @Androidicus but how will I do I also want other components on top of video screen in horizontal layout like volume controls fwd next one more button to stop full screen how will I do that – Erum Sep 16 '15 at 16:32
  • @Androidicus see I want this – Erum Sep 16 '15 at 16:33
  • This is not working I used your code below is my code – Ganpat Kaliya Aug 08 '16 at 09:01
  • RelativeLayout is my Parent layout of videioview. and below is my java code. – Ganpat Kaliya Aug 08 '16 at 09:04
  • DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int height = displaymetrics.heightPixels; int width = displaymetrics.widthPixels; RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height); myVideo.setLayoutParams(lp); – Ganpat Kaliya Aug 08 '16 at 09:04
  • I am using any one code either xml or java but its not working so what is actual proble ? – Ganpat Kaliya Aug 08 '16 at 09:05
26

I have done this way:

Check these reference screen shots.

enter image description here

Add class FullScreenVideoView.java:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;

public class FullScreenVideoView extends VideoView {
    public FullScreenVideoView(Context context) {
        super(context);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
}

How to bind with xml:

<FrameLayout
   android:id="@+id/secondMedia"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

     <com.my.package.customview.FullScreenVideoView
           android:layout_width="match_parent"
           android:layout_height="match_parent" 
           android:id="@+id/fullScreenVideoView"/>

</FrameLayout>

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
15

First Method

when you want to open a video in full screen for that Activity you have to set the theme attribute in the Manifest. set this value that is

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

change theme programmatically here

Second Method

create another fullscreen.xml like below and setContentView(R.layout.fullscreen) on click of the button

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

    <VideoView android:id="@+id/myvideoview"
        android:layout_width="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_height="fill_parent">
    </VideoView>

</RelativeLayout>
Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
9

On Button click start the native video player which will open in full screen:

Intent intent = new Intent(Intent.ACTION_VIEW );
intent.setDataAndType(Uri.parse(path), "video/*");
startActivity(intent);
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
2

The below code worked.

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

Add this code before calling videoView.start(). With this the video activity runs in full screen mode in most of the cases. But if the title bar is still displayed then change your theme in your manifest to this.

    android:theme="@style/Theme.AppCompat.NoActionBar">
saketha yellanki
  • 153
  • 2
  • 10
1

you can achieve it by creating two separate activity. Suppose first activity is halfScreen activity. In this activity your video view having small size. On button click of full screen video start another activity 'fullScreen activity'. In second activity the video view should be match parent to the parent layout.you can also start video in full screen from where it is paused in half screen.In my code i have implemented that.Also you can resume the video in half screen on the back press of fullscreen activity. This is work for me.Hope it will work for you also.

Here is the code
half.xml

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

    <VideoView
        android:id="@+id/VideoViewhalf"
        android:layout_width="match_parent"
        android:layout_height="300dp" >
    </VideoView>

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnfullScreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fullscreen" />

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </LinearLayout>

</LinearLayout>

HalfScreen activity
public class HalfScreen extends Activity {
    Button btn;
    VideoView videoView = null;
    final int REQUEST_CODE = 5000;
    final String videoToPlay = "http://bffmedia.com/bigbunny.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.half);
        videoView = (VideoView) findViewById(R.id.VideoViewhalf);
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        btn = (Button) findViewById(R.id.btnfullScreen);

        Uri video = Uri.parse(videoToPlay);
        videoView.setVideoURI(video);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                progressBar.setVisibility(View.GONE);
                videoView.requestFocus();
                videoView.start();
            }
        });

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent videointent = new Intent(HalfScreen.this,
                        FullScreen.class);
                videointent.putExtra("currenttime",
                        videoView.getCurrentPosition());
                videointent.putExtra("Url", videoToPlay);
                startActivityForResult(videointent, REQUEST_CODE);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            if (data.hasExtra("currenttime")) {
                int result = data.getExtras().getInt("currenttime", 0);
                if (result > 0) {
                    if (null != videoView) {
                        videoView.start();
                        videoView.seekTo(result);
                        ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
                        progressBar.setVisibility(View.VISIBLE);

                    }
                }
            }
        }
    }
}

full.xml

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

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

</LinearLayout>

FullScreen Activity

public class FullScreen extends Activity {
    Button btn;
    VideoView videoView = null;
    int currenttime = 0;
    String Url = "";
    private static ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        if (null != extras) {
            currenttime = extras.getInt("currenttime", 0);
            Url = extras.getString("Url");
        }
        setContentView(R.layout.full);
        progressDialog = ProgressDialog.show(this, "", "Loading...", true);
        videoView = (VideoView) findViewById(R.id.VideoViewfull);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse(Url);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);

        videoView.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer arg0) {
                progressDialog.dismiss();
                videoView.start();
                videoView.seekTo(currenttime);
            }
        });
    }

    @Override
    public void finish() {
        Intent data = new Intent();
        data.putExtra("currenttime", videoView.getCurrentPosition());
        setResult(RESULT_OK, data);
        super.finish();
    }
}
Chandra Sharma
  • 1,339
  • 1
  • 12
  • 26
1

I achieved it by switching to landscape orientation and setting layout params to MATCH_PARENT. Just before switching to fullscreen mode, we need to store current orientation mode and VideoView params indefaultScreenOrientationMode and defaultVideoViewParams variables correspondingly. So that we can use them when we exit from video fullscreen mode. Thus, when you want to open video in fullscreen mode, use makeVideoFullScreen() method, to exit - exitVideoFullScreen().

Please, note I used RelativeLayout for my VideoView, in your case it can be another layout type.

private RelativeLayout.LayoutParams defaultVideoViewParams;
private int defaultScreenOrientationMode;

// play video in fullscreen mode
private void makeVideoFullScreen() {

    defaultScreenOrientationMode = getResources().getConfiguration().orientation;
    defaultVideoViewParams = (LayoutParams) videoView.getLayoutParams();

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    videoView.postDelayed(new Runnable() {

        @Override
        public void run() {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);

            videoView.setLayoutParams(params);
            videoView.layout(10, 10, 10, 10);
        }
    }, 700);
}


// close fullscreen mode
private void exitVideoFullScreen() {
    setRequestedOrientation(defaultScreenOrientationMode);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

    videoView.postDelayed(new Runnable() {

        @Override
        public void run() {
            videoView.setLayoutParams(defaultVideoViewParams);
            videoView.layout(10, 10, 10, 10);
        }
    }, 700);
}
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
1

whether you want to keep the aspect ratio of a video or stretch it to fill its parent area, using the right layout manager can get the job done.

Keep the aspect ratio :

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<VideoView
  android:id="@+id/videoView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_gravity="center"/>

</LinearLayout>

!!! To fill in the field:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <VideoView android:id="@+id/videoViewRelative"
         android:layout_alignParentTop="true"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
    </VideoView>

</RelativeLayout>
K. Rea
  • 5
  • 2
1
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) mVideoView.getLayoutParams();
params.width = (int) metrics.widthPixels;
params.height = (int) metrics.heightPixels;
mVideoView.setLayoutParams(params);

playVideo();
aspectRatio = VideoInfo.AR_4_3_FIT_PARENT;
mVideoView.getPlayer().aspectRatio(aspectRatio);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

Try code below here.

if (!isFullScreen())
        {
            Log.v("Full screen", "-----------is full screen------------");  
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
            DisplayMetrics displaymetrics = new DisplayMetrics();
              getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
              int height = displaymetrics.heightPixels;
              int width = displaymetrics.widthPixels;
              android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
              params.width = width;
              params.height=height;
              params.setMargins(0, 0, 0, 0);

        }
        else{
            Log.v("Full screen", "-----------small screen------------");    

            DisplayMetrics displaymetrics = new DisplayMetrics();
              getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
              int height = displaymetrics.heightPixels;
              int width = displaymetrics.widthPixels;
              android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
              params.width = width;
              params.height=height / 3;
              params.setMargins(0, 0, 0, 0);
        }
Do Xuan Nguyen
  • 189
  • 4
  • 8
0

This code is for full screen landscape video

AndroidManifext.xml (Setting the orientation)

        <activity
        android:name=".Video1"
        android:screenOrientation="landscape" />

Video1.java Code :

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class Video1 extends AppCompatActivity {

private VideoView videoView;
private MediaController mediaController;

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

    videoView = findViewById(R.id.videoView);
    String fullScreen =  getIntent().getStringExtra("fullScreenInd");
    if("y".equals(fullScreen)){
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
    }

    Uri videoUri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.YOUR_VIDEO_NAME);

    videoView.setVideoURI(videoUri);

    mediaController = new FullScreenMediaController(this);
    mediaController.setAnchorView(videoView);

    videoView.setMediaController(mediaController);
    videoView.start();
    }
}

FullScreenMediaControler.java Code :

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.MediaController;

public class FullScreenMediaController extends MediaController {

private ImageButton fullScreen;
private String isFullScreen;

public FullScreenMediaController(Context context) {
    super(context);
}

@Override
public void setAnchorView(View view) {

    super.setAnchorView(view);

    //image button for full screen to be added to media controller
    fullScreen = new ImageButton (super.getContext());

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.RIGHT;
    params.rightMargin = 80;
    addView(fullScreen, params);

    //fullscreen indicator from intent
    isFullScreen =  ((Activity)getContext()).getIntent().
            getStringExtra("fullScreenInd");

    if("y".equals(isFullScreen)){
        fullScreen.setImageResource(R.drawable.ic_fullscreen_exit);
    }else{
        fullScreen.setImageResource(R.drawable.ic_fullscreen);
    }

    //add listener to image button to handle full screen and exit full screen events
    fullScreen.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(getContext(),Video1.class);

            if("y".equals(isFullScreen)){
                intent.putExtra("fullScreenInd", "");
            }else{
                intent.putExtra("fullScreenInd", "y");
            }
            ((Activity)getContext()).startActivity(intent);
        }
    });
  }
}
Mikhael Lauda
  • 71
  • 1
  • 4
0
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".auth.fragments.SplashScreenFragment">


<VideoView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Peter
  • 587
  • 6
  • 16