Home » Android » android – Recompose Activity layout to play video in fullscreen when screen is rotated

android – Recompose Activity layout to play video in fullscreen when screen is rotated

Posted by: admin June 15, 2020 Leave a comment


In an Activity I have embedded a Video widget (VideoView or MediaPlayer associated to a SurfaceView) which size should be adapted when the screen is rotated. This Activity is used as content in a TabHost.

My current Approach

In order to handle screen rotation I have provided two layouts one for portrait and another for landscape orientation (placed in ./res/layout and ./res/layout-land folders).

Problem of this approach is that a new Activity is created for each rotation so I don’t use the same MediaPlayer (or VideoView) => I start the video from the beginning every time I rotate the screen. As the video is streamed from the web there is no way to store the position in the video in order to seek to this position when restarting the video in the other Activity.

What I would like to do

I need to be able to translate/scale/hide views inside the Activity when screen is rotated. Translation/scaling needs to maintain layout organisation in order to have a layout adapted to different screen sizes.

Desired animation/layout

My questions

  • How to translate/move views to the top-left position of the screen ?
  • How to hide/translate View outside of the screen in order to make a View disappear ?
  • How to scale/resize View to adjust its size to available space around it ?

  • Which interface, callback do I need to implement in order to be notified of screen rotation to fire these transformations ?

How to&Answers:

I finally find the good way to do it.
In Android documentation it is specified that when you need to handle modifications (orientation, keyboard …) without recreating a new Activity you should do it by overriding the onConfigurationChanged method of the Activity class. You have to specify modifications you are sensible in the manifest file of your Activity.

You can find more information about this here.

In my case, my Activity manifest looks like:

<activity android:name=".MyActivity" android:configChanges="orientation"></activity> 

And inside my MyActivity Activity I have added the following method:

    public void onConfigurationChanged(Configuration newConfig) {
        int visibility = View.VISIBLE;
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            visibility = View.GONE;

with the following layout for my Activity:

<?xml version="1.0" encoding="utf-8"?>


          android:layout_height="wrap_content" />

          android:layout_height="fill_parent" />



This way, when I hide/show the tabWidget bar, the VideoView (which is added to the content of the TabHost is resized and when in landscape, I have the fullscreen to watch the movie.

I hope it will help some of you.