Starting in Android 8.0 (API level 26), Android allows activities to launch in picture-in-picture (PiP) mode. PiP is a special type of multi-window mode mostly used for video playback. It lets the user watch a video in a small window pinned to a corner of the screen while navigating between apps or browsing content on the main screen.
PiP leverages the multi-window APIs made available in Android 7.0 to provide the pinned video overlay window. To add PiP to your app, you need to register your activities that support PiP, switch your activity to PiP mode as needed, and make sure UI elements are hidden and video playback continues when the activity is in PiP mode.
The PiP window appears in the topmost layer of the screen, in a corner chosen by the system.
How users can interact with the PiP window
Users can drag the PiP window to another location. Starting in Android 12, users can also:
Single-tap the window to display a full-screen toggle, a close button, a settings button, and custom actions provided by your app (for example, play controls).
Double-tap the window to toggle between the current PiP size and maximum PiP size. Stash the window by dragging it to the left or right edge; to unstash the window, either tap the visible part of the stashed window or drag it out.
Resize the PiP window using pinch-to-zoom.
Your app controls when the current activity enters PiP mode. Here are some examples:
An activity can enter PiP mode when the user taps the home button (in button navigation mode) or swipes up to home (in gesture navigation mode). (This is how Google Maps continues to display directions while the user runs another activity at the same time.)
Your app can move a video into PiP mode when the user navigates back from the video to browse other content.
Your app can switch a video into PiP mode while a user watches the end of an episode of content. The main screen displays promotional or summary information about the next episode in the series.
Your app can provide a way for users to queue up additional content while they watch a video. The video continues playing in PiP mode while the main screen displays a content selection activity.
Declare picture-in-picture support
By default, the system does not automatically support PiP for apps.
If you want support PiP in your app, register your video
activity in your manifest by setting
android:supportsPictureInPicture
to true
. Also, specify
that your activity handles layout configuration changes so that your activity
doesn't relaunch when layout changes occur during PiP mode transitions.
<activity android:name="VideoActivity"
android:supportsPictureInPicture="true"
android:configChanges=
"screenSize|smallestScreenSize|screenLayout|orientation"
...
Switch your activity to picture-in-picture
To enter picture-in-picture mode, an activity must call
enterPictureInPictureMode()
.
For example, the following
code switches an activity to PiP mode when the user clicks a dedicated button in
the app's UI:
Kotlin
override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } }
Java
@Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... }
You might want to include logic that switches an activty into PiP mode instead
of going into the background. For example, Google Maps switches to PiP mode if
the user presses the home or recents button while the app is navigating. You can
catch this case by overriding
onUserLeaveHint()
:
Kotlin
override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } }
Java
@Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } }
Make transitions to PiP mode smoother from gesture navigation
Starting in Android 12, you can use the
setAutoEnterEnabled
flag to provide smoother transitions to PiP mode when swiping up to home in
gesture navigation mode.
To implement this feature:
Use
setAutoEnterEnabled
to constructPictureInPictureParams.Builder
, as follows:setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build());
Call
setPictureInPictureParams
with the up-to-datePictureInPictureParams
early. The app should not wait for theonUserLeaveHint
callback (as it would have done in Android 11).For example, an app may want to call
setPictureInPictureParams
on the very first playback and any following playback if the aspect ratio is changed.Call
setAutoEnterEnabled(false)
as needed. For example, it’s probably not optimal for a video app to enter PiP if the current playback is in paused state.
Handle UI during picture-in-picture
When the activity enters or exits picture-in-picture mode, the system calls
Activity.onPictureInPictureModeChanged()
or Fragment.onPictureInPictureModeChanged()
.
You should override these callbacks to redraw the activity's UI elements. Keep in mind that in PiP mode your activity is shown in a small window. Users cannot interact with your app's UI elements when it's in PiP mode and the details of small UI elements may be difficult to see. Video playback activities with minimal UI provide the best user experience.
If your app needs to provide custom actions for PiP, see Add controls in this document. Remove other UI elements before your activity enters PiP and restore them when your activity becomes fullscreen again:
Kotlin
override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration) { if (isInPictureInPictureMode) { // Hide the full-screen UI (controls, etc.) while in picture-in-picture mode. } else { // Restore the full-screen UI. } }
Java
@Override public void onPictureInPictureModeChanged (boolean isInPictureInPictureMode, Configuration newConfig) { if (isInPictureInPictureMode) { // Hide the full-screen UI (controls, etc.) while in picture-in-picture mode. } else { // Restore the full-screen UI. ... } }
Support smoother animations when exiting out of PiP mode
Starting in Android 12, the SourceRectHint
flag is now reused to implement smoother animation when exiting out of PiP mode.
On exit, the system creates the animation using the current available
sourceRectHint
, whether it’s the original Rect
used to enter PiP or an
updated Rect
provided by the app.
To implement this feature, update your app as follows:
Continue to construct
PictureInPictureParams
with thesourceRectHint
andaspectRatio
for a smooth entry animation.If necessary, update the
sourceRectHint
before the system starts the exit transition. When the system is about to exit PiP mode, the activity’s view hierarchy is laid out to its destination configuration (for example, full screen). The app can attach a layout change listener to its root view or target view (such as the video player view) to detect the event and update the sourceRectHint before the animation begins.Kotlin
// Listener is called immediately after the user exits PiP but before animating. playerView.addOnLayoutChangeListener { _, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom -> if (left != oldLeft || right != oldRight || top != oldTop || bottom != oldBottom) { // The playerView's bounds changed, update the source hint rect to // reflect its new bounds. val sourceRectHint = Rect() playerView.getGlobalVisibleRect(sourceRectHint) setPictureInPictureParams( PictureInPictureParams.Builder() .setSourceRectHint(sourceRectHint) .build() ) } }
Java
// Listener is called right after the user exits PiP but before // animating. playerView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> { if (left != oldLeft || right != oldRight || top != oldTop || bottom != oldBottom) { // The playerView’s bounds changed, update the source hint rect to // reflect its new bounds. final Rect sourceRectHint = new Rect(); playerView.getGlobalVisibleRect(sourceRectHint); setPictureInPictureParams( new PictureInPictureParams.Builder() .setSourceRectHint(sourceRectHint) .build()); } });
Add controls
The PiP window can display controls when the user opens the window's menu (by tapping the window on a mobile device, or selecting the menu from the TV remote.)
If an app has an active media session, then play, pause, next, and previous controls will appear.
You can also specify custom actions explicitly by building
PictureInPictureParams
with PictureInPictureParams.Builder.setActions()
before entering PiP mode, and pass the params when you enter PiP mode using
enterPictureInPictureMode(android.app.PictureInPictureParams)
or setPictureInPictureParams(android.app.PictureInPictureParams)
.
Be careful. If you try to add more than
getMaxNumPictureInPictureActions()
,
you'll only get the maximum number.
Disable seamless resizing for non-video content
Android 12 adds the setSeamlessResizeEnabled
flag, which provides a much smoother cross-fading animation when resizing
non-video content in the PiP window. Previously, resizing non-video content in a
PiP window could create jarring visual artifacts.
The setSeamlessResizeEnabled
flag is set to true
by default for
backward-compatibility. Leave this set to true
for video content, and change
it to false
for non-video content.
To disable seamless resizing for non-video content:
setPictureInPictureParams(new PictureInPictureParams.Builder()
.setSeamlessResizeEnabled(false)
.build());
Continuing video playback while in picture-in-picture
When your activity switches to PiP, the system places the activity in the
paused state and calls the activity's
onPause()
method. Video
playback should not be paused and should continue playing if the activity is
paused while in PiP mode.
In Android 7.0 and later, you should pause and resume video playback when the
system calls your activity's
onStop()
and
onStart()
. By doing this,
you can avoid having to check if your app is in PiP mode in onPause() and
explicitly continuing playback.
If you have to pause playback in your onPause()
implementation, check for PiP
mode by calling isInPictureInPictureMode()
and handle playback appropriately,
for example:
Kotlin
override fun onPause() { super.onPause() // If called while in PiP mode, do not pause playback if (isInPictureInPictureMode) { // Continue playback } else { // Use existing playback logic for paused Activity behavior. } }
Java
@Override public void onPause() { // If called while in PiP mode, do not pause playback if (isInPictureInPictureMode()) { // Continue playback ... } else { // Use existing playback logic for paused Activity behavior. ... } }
When your activity switches out of PiP mode back to full-screen mode, the
system resumes your activity and calls your
onResume()
method.
Using a single playback activity for picture-in-picture
In your app, a user might select a new video when browsing for content on the main screen, while a video playback activity is in PiP mode. Play the new video in the existing playback activity in full screen mode, instead of launching a new activity that might confuse the user.
To ensure a single activity is used for video playback requests and
switched into or out of PiP mode as needed, set the activity's
android:launchMode
to singleTask
in your manifest:
<activity android:name="VideoActivity"
...
android:supportsPictureInPicture="true"
android:launchMode="singleTask"
...
In your activity, override
onNewIntent()
and handle the new video, stopping any existing video playback if needed.
Best practices
PiP might be disabled on devices that have low RAM. Before your app uses PiP,
check to be sure it is available by calling
hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
.
PiP is intended for activities that play full-screen video. When switching your activity into PiP mode, avoid showing anything except video content. Track when your activity enters PiP mode and hide UI elements, as described in Handling UI during picture-in-picture.
When an activity is in PiP mode, by default it doesn't get input focus. To
receive input events while in PiP mode, use
MediaSession.setCallback()
.
For more information on using setCallback()
see
Display a Now Playing card.
When your app is in PiP mode, video playback in the PiP window can cause audio interference with another app, such as a music player app or voice search app. To avoid this, request audio focus when you start playing the video, and handle audio focus change notifications, as described in Managing Audio Focus. If you receive notification of audio focus loss when in PiP mode, pause or stop video playback.
When your app is about to enter PiP, note only the top activity will enter
Picture-in-Picture. In some situations such as on multi-window devices, it is
possible the activity below will now be shown and become visible again alongside
the PiP activity. You should handle this case accordingly, including the
activity below getting an onResume()
or an onPause()
callback. It is also
possible that the user may interact with the activity. For example, if you have
a video list activity displayed and the playing video activity in PiP mode, the
user might select a new video from the list and the PiP activity should update
accordingly.
Additional sample code
To download a sample app written in Android, see Picture-in-Picture Sample. To download a sample app written in Kotlin, see Android PictureInPicture Sample (Kotlin).