Search code examples
androidfullscreenandroid-videoviewlandscape

Android - Fullscreen Layout for VideoView


I want to show my Video with normal size on portrait, and with Full Size on Landscape. So when the users starts to watch at portrait and switches to Landscape(Rotates the device) It goes fullscreen; when vice versa, It goes back to original size. I've checked internet but couldn't find a good example. My codes are below.

enter image description here enter image description here

VideoActivity.java:

public class VideoActivity extends Activity {

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_video);


        Bundle bund = getIntent().getExtras();
        String s = bund.getString("LINK");
        VideoView videoView = (VideoView) findViewById(R.id.videoView);
        MediaController controller = new MediaController(this);
        videoView.setVideoPath(s);
        videoView.setMediaController(controller);
        videoView.start();
    }
}

activity_video.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}"
    android:background="#000000"
    >

    <VideoView
        android:id="@+id/videoView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"

         />

</RelativeLayout>

Basically. When user starts the video it can be original size (If Portrait Mode) but whenever he turns the phone around I want it to go fullscreen automatically. And when turned back to portrait, original size again.

Thanks.


Solution

  • Create a new file named bools.xml in values folder as below:

    <?xml version="1.0" encoding="utf-8"?>
        <resources>
        <item type="bool" name="isPortLayout">true</item>
    </resources>
    

    create another file in values-land folder named bools.xml as below:

    <?xml version="1.0" encoding="utf-8"?>
        <resources>
        <item type="bool" name="isPortLayout">false</item>
    </resources>
    

    Now in your activity before calling to setContentView get this resource and based on its value set Activity as full screen or not:

    boolean isPortLayout = getResources().getBoolean(R.bool.isLargeLayout);
    if(isPortLayout) {
        // PORT
    } else {
        // LAND
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    

    Set your VideoView's width and height match_parent.