Skip to main content

Picture in picture

A newer version of the Maps SDK is available
This page uses v9.7.1 of the Mapbox Maps SDK. A newer version of the SDK is available. Learn about the latest version, v11.8.1, in the Maps SDK documentation.
Note

This example is a part of the Mapbox Android Demo app. You can find the values for all referenced resources in the res directory. For example, see res/values/activity_strings.xml for R.string.* references used in this example. The dependencies can be found here.The examples use View binding.See setup documention if necessary.

activity_picture_in_picture
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_window_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_crop_square_white_24dp"
mapbox:fabSize="normal"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintRight_toRightOf="parent"/>


<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="13.7563309"
mapbox:mapbox_cameraTargetLng="100.501765"
mapbox:mapbox_cameraTilt="0"
mapbox:mapbox_cameraZoom="3"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"/>


</androidx.constraintlayout.widget.ConstraintLayout>
PictureInPictureActivity.java
package com.mapbox.mapboxandroiddemo.examples.labs;

import android.os.Bundle;
import androidx.annotation.NonNull;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;

/**
* Enter picture-in-picture mode with a map being persisted. Only works on devices running Android O and above.
*/
public class PictureInPictureActivity extends AppCompatActivity {

private MapView mapView;
private FloatingActionButton addPictureFab;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_picture_in_picture);

mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.SATELLITE_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
addPictureFab = findViewById(R.id.add_window_fab);
addPictureFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
enterPictureInPictureMode();
} catch (Exception exception) {
Toast.makeText(PictureInPictureActivity.this, R.string.no_picture_in_picture_support,
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
});
}

@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode);

addPictureFab.setVisibility(isInPictureInPictureMode ? View.GONE : View.VISIBLE);

if (isInPictureInPictureMode && getSupportActionBar() != null) {
// Hide the controls in picture-in-picture mode.
getSupportActionBar().hide();
} else {
// Restore the playback UI based on the playback status.
if (getSupportActionBar() != null) {
getSupportActionBar().show();
}
}
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}