Android Oreo: Picture In Picture Mode

James O'Brien
jamesob.com
Published in
3 min readSep 6, 2017
Incalculable productivity improvement

The recent release of Android Oreo added a bunch of new features to the operating system, and one of my favourites is called Picture In Picture. This allows the user to shrink an Activity into the corner of the screen, and navigate away from the application while still being able to see the minimised content.

Requirements

  • Android SDK v25, however some methods were introduced with v26
  • Android Build Tools v25.0.3

Preparing your Activity

Activities must define that they support the Picture In Picture mode in the Android Manifest. It is also important that the activity specifies that it handles configuration changes so the activity isn’t relaunched after layout changes.

<activity            
android:name=".PictureInPictureActivity"
android:supportsPictureInPicture="true"
android:configChanges=
"screenSize|smallestScreenSize|screenLayout|orientation

When in Picture In Picture mode, it’s possible to launch the same activity again. I would recommend setting the Activity’s launch mode to android:launchMode="singleTask” to avoid confusing the user and having multiple instances of the same screen.

One gotcha in relation to Picture In Picture is that the activity is paused when entering the mode. In any multi-window mode, only the activity the user has most recently interacted with is considered active and all other activities are in the paused state. This means that the stopping of video playback, or any similar functionality, should be moved from onPause() to onStop().

More information on this can be found here: Multi-Window Lifecycle.

Entering Picture In Picture Mode

All you really need to do is call enterPictureInPictureMode(). The activity will then automatically animate into position.

There’s no predefined way to display this functionality to the user. If you were displaying a video, it could be shown as an action alongside the play/stop buttons. Alternatively, you could add it to the action bar menu item similar to the google cast button.

Fortunately, Google provides suggested iconography — https://material.io/icons/

There’s also enterPictureInPictureMode(PictureInPictureParams) which allows you to define some parameters for the Picture In Picture mode when launching.

Monitor Changes

You can monitor the current state of the Picture in Picture mode using a combination of onPictureInPictureModeChanged() to be notified of changes and isInPictureInPictureMode() which reveals the current state.

@Override
void onPictureInPictureModeChanged(boolean isEnabled,
Configuration newConfig) {
Assert.assertEquals(isEnabled, isInPictureInPictureMode());
}

A good use case would be changing the visibility of secondary views between modes. It’s recommended that when in Picture In Picture mode you should avoid showing anything except the main content. For example, the video content would remain visible but the title and description should be hidden.

Maintaining Aspect Ratio

By default Picture in Picture will display as a horizontal rectangle in the bottom right of the screen. This can be altered by providing an aspect ratio as a parameter.

We touched on PictureInPictureParams earlier in the article, and we will be using them to set the aspect ratio of the minimised view with the setAspectRatio method. This doesn’t need to be complicated; if you wish to maintain the current ratio just pass the width and height of the view, and the rest will be taken care of.

aspectRatio = new Rational(view.getWidth(), view.getHeight());params = new PictureInPictureParams.Builder()
.setAspectRatio(aspectRatio)
.build();

These parameters can be set with setPictureInPictureParams() or by being passed as an argument when entering the mode.

There are also more PictureInPictureParams available such as setting a number of actions to be displayed while minimised and identifying the video bounds for animating the view.

Final Thoughts

We will likely be well into 2018 before Android Oreo’s adoption is considered significant, but thinking about these types of features when designing your applications can help with simplification and usability.

I know I’m guilty of browsing twitter etc on my phone while also watching the television, and I don’t think it will be long before people will want the same ability on their personal devices.

--

--