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

Questions:

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:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        int visibility = View.VISIBLE;
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            visibility = View.GONE;
        }
        getTabHost().getTabWidget().setVisibility(visibility);
    }

with the following layout for my Activity:

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

    <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

      <TabWidget
          android:id="@android:id/tabs"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" />

      <FrameLayout
          android:id="@android:id/tabcontent"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" />

    </LinearLayout>

</TabHost>

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.