In this article, it is explained how to implement a Picture in Picture (PIP) in an Android application.
We have seen in many apps such as Google Maps while using navigation that when we close the app, there is a floating screen that appears at the bottom right of the screen as shown in the image below. This screen is known as PIP(Picture in Picture) mode.
What is PIP(Picture in Picture) mode?
PIP is a special type of multi-window mode mainly used for activities that need to be active on screen but should not take up the whole screen space like watching videos, video calls, navigation, etc. It lets the user watch a video in a small window pinned to a corner of the screen (by default bottom right) while navigating between apps or browsing content on the main screen. Android 8.0 (API level 26) and above allows activities to launch in PIP mode.
The PIP window appears in the top-most layer of the screen. You can drag the PIP window to another location using some special toggles. When you tap on the window two special controls appear:
- a full-screen toggle (in the centre of the window) and
- a close button (an “X” in the upper right corner).
Below is the implementation of the PIP mode.
- Create an android project in android studio.
- Declaring picture-in-picture support: By default, no activity has PIP mode enabled. This needs to be done via the Manifest file
<activity android:name="VideoActivity" android:supportsPictureInPicture="true" android:configChanges= "screenSize|smallestScreenSize|screenLayout|orientation" ...
- Now, in the layout file (activity_main.xml), we will have two components in the activity:
- Now, let’s add some code in MainActivity.java file. In this app, we will change the activity to PIP mode on a button click.
- First, we will get the display size using the getWindowManager().
- After that using the function enterPictureInPictureMode() which should be provided with a PictureInPictureParams.Builder parameter.
Below is the code for the same.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Create an Expandable Notification Containing a Picture in Android
- How to implement Swipe Down to Refresh in Android using Android Studio
- Post a picture automatically on Instagram using Python
- How to implement View Shaker in Android
- How to implement Options Menu in Android
- How to implement Dark (Night) mode in Android app
- How to implement a TabLayout in Android using ViewPager and Fragments
- Implement Zoom In or Zoom Out in Android
- Implement customized TimePicker in Android using SnapTimePicker
- Implement reverse AnimatedVectorDrawables using MorphView in Android
- How to Implement Press Back Again to Exit in Android?
- Android | AdMob Banner Ads for Android Studio
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.