4

I'm trying to replicate in my app what the Youtube app does when playing a video.

Portrait mode: Shows the video using only 40% of the screen and the rest with information about the video.

Landscape mode: Shows the video and the window in fullscreen mode.

I've seen a lot of threads in stack overflow and other forums about this subject, but none of them seem to work with me. Currently i have my videoplayeractivity with android:configChanges="keyboardHidden|orientation|screenSize" and i've overriden the onConfigurationChanged event with the following code:

@Override
public void onConfigurationChanged(Configuration config)
{
    super.onConfigurationChanged(config);

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }
    else
    {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    }
}

This doesn't put the app in fullscreen, and if i uncomment the requestwindowfeature it will crash because "requestFeature() must be called before adding content". Later on i've tried removing the onConfigurationChanged event and on the onCreate event i've tried :

@Override
public void onCreate(Bundle savedInstanceState)
{
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_video);

But crashes with NullPointerException at com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.onRestoreInstanceState(PhoneWindow.java:3527) if i start the activity on portrait and then flip to landscape. Any suggestions?

xlar8or
  • 601
  • 9
  • 18

1 Answers1

-1

To put it in the fullscreen:

video_layout.xml:

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

Activity:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
   }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  }

And to support orientation changes like youtube app: Toggling fullscreen & orientation like YouTube

Andy
  • 953
  • 1
  • 8
  • 18