Add PiP through a button
Stay organized with collections
Save and categorize content based on your preferences.
To enter PiP mode through a button click, call
enterPictureInPictureMode()
on findActivity()
.
The parameters are already set by previous calls to the
PictureInPictureParams.Builder
, so you do not need to set new parameters
on the builder. However, if you do want to change any parameters on button
click, you can set them here.
val context = LocalContext.current
Button(onClick = {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.findActivity().enterPictureInPictureMode(
PictureInPictureParams.Builder().build()
)
} else {
Log.i(PIP_TAG, "API does not support PiP")
}
}) {
Text(text = "Enter PiP mode!")
}
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-05-20 UTC.
[null,null,["Last updated 2025-05-20 UTC."],[],[],null,["# Add PiP through a button\n\nTo enter PiP mode through a button click, call\n[`enterPictureInPictureMode()`](/reference/android/app/Activity#enterPictureInPictureMode(android.app.PictureInPictureParams)) on `findActivity()`.\n\nThe parameters are already set by previous calls to the\n[`PictureInPictureParams.Builder`](/reference/android/app/Activity#enterPictureInPictureMode(android.app.PictureInPictureParams)), so you do not need to set new parameters\non the builder. However, if you do want to change any parameters on button\nclick, you can set them here.\n\n\n```kotlin\nval context = LocalContext.current\nButton(onClick = {\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.O) {\n context.findActivity().enterPictureInPictureMode(\n PictureInPictureParams.Builder().build()\n )\n } else {\n Log.i(PIP_TAG, \"API does not support PiP\")\n }\n}) {\n Text(text = \"Enter PiP mode!\")\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt#L103-L114\n```\n\n\u003cbr /\u003e"]]