Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2025 Google LLC
Copyright 2026 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,31 +50,30 @@
map:latLngBoundsSouthWestLongitude="-122.1" />

<LinearLayout
android:background="@color/white"
android:background="?attr/colorSurface"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_bar">

<LinearLayout
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingHorizontal="4dp"
android:orientation="horizontal">

<com.google.android.material.button.MaterialButton
android:id="@+id/clamp_min_zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clamp_min_zoom" />

<com.google.android.material.button.MaterialButton
android:id="@+id/clamp_max_zoom"
android:layout_width="wrap_content"
<TextView
android:id="@+id/zoom_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/clamp_max_zoom" />
android:layout_weight="1"
android:textAppearance="?attr/textAppearanceBodyMedium"
android:text="@string/initial_zoom_limits" />

<com.google.android.material.button.MaterialButton
android:id="@+id/clamp_zoom_reset"
Expand All @@ -83,43 +82,63 @@
android:text="@string/clamp_zoom_reset" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
<com.google.android.material.slider.RangeSlider
android:id="@+id/zoom_range_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:valueFrom="2.0"
android:valueTo="21.0"
android:stepSize="0.5"
app:values="@array/initial_zoom_range" />

<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/latlng_clamp_toggle_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:singleSelection="true"
app:selectionRequired="false">

<com.google.android.material.button.MaterialButton
android:id="@+id/clamp_latlng_adelaide"
android:layout_width="wrap_content"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_weight="1"
android:text="@string/clamp_latlng_adelaide" />

<com.google.android.material.button.MaterialButton
android:id="@+id/clamp_latlng_pacific"
android:layout_width="wrap_content"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_weight="1"
android:text="@string/clamp_latlng_pacific" />

<com.google.android.material.button.MaterialButton
android:id="@+id/clamp_latlng_reset"
android:layout_width="wrap_content"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/clamp_latlng_reset" />
</LinearLayout>
</com.google.android.material.button.MaterialButtonToggleGroup>

<LinearLayout
android:layout_width="wrap_content"
<TextView
android:id="@+id/clamp_status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/camera_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/move_the_camera" />
</LinearLayout>
android:paddingHorizontal="4dp"
android:paddingTop="4dp"
android:textAppearance="?attr/textAppearanceBodyMedium"
android:text="@string/latlng_clamp_status_none" />

<TextView
android:id="@+id/camera_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="4dp"
android:paddingTop="2dp"
android:text="@string/move_the_camera" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
10 changes: 5 additions & 5 deletions ApiDemos/project/common-ui/src/main/res/layout/camera_demo.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2025 Google LLC
Copyright 2026 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@string/background_color_customization_demo_label"
app:title="@string/camera_demo_label"
app:titleTextColor="?attr/colorOnPrimary" />

<androidx.fragment.app.FragmentContainerView
Expand All @@ -40,7 +40,7 @@
android:id="@+id/first_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:background="?attr/colorSurface"
android:baselineAligned="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down Expand Up @@ -160,7 +160,7 @@
android:id="@+id/second_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:background="?attr/colorSurface"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/first_container">
Expand All @@ -183,7 +183,7 @@
android:id="@+id/third_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:background="?attr/colorSurface"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/second_container">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,13 +31,13 @@
* This shows how to constrain the camera to specific boundaries and zoom levels.
*/
public class CameraClampingDemoActivity extends SamplesBaseActivity
implements OnMapReadyCallback, OnCameraIdleListener {
implements OnMapReadyCallback, OnCameraIdleListener, GoogleMap.OnCameraMoveListener {

private static final String TAG = CameraClampingDemoActivity.class.getSimpleName();

private static final float ZOOM_DELTA = 2.0f;
private static final float DEFAULT_MIN_ZOOM = 2.0f;
private static final float DEFAULT_MAX_ZOOM = 22.0f;
private static final float DEFAULT_MAX_ZOOM = 21.0f;

static final LatLngBounds ADELAIDE_BOUNDS = new LatLngBounds(
new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
Expand Down Expand Up @@ -71,18 +71,27 @@ protected void onCreate(Bundle savedInstanceState) {

mMap = null;
resetMinMaxZoom();
updateZoomLabel(mMinZoom, mMaxZoom);

binding.zoomRangeSlider.addOnChangeListener((slider, value, fromUser) -> {
java.util.List<Float> values = slider.getValues();
mMinZoom = values.get(0);
mMaxZoom = values.get(1);
updateZoomLabel(mMinZoom, mMaxZoom);
if (fromUser && mMap != null) {
mMap.setMinZoomPreference(mMinZoom);
mMap.setMaxZoomPreference(mMaxZoom);
}
});

binding.clampLatlngAdelaide.setOnClickListener(v -> onClampToAdelaide());
binding.clampLatlngPacific.setOnClickListener(v -> onClampToPacific());
binding.clampLatlngReset.setOnClickListener(v -> onLatLngClampReset());
binding.clampMinZoom.setOnClickListener(v -> onSetMinZoomClamp());
binding.clampMaxZoom.setOnClickListener(v -> onSetMaxZoomClamp());
binding.clampZoomReset.setOnClickListener(v -> onMinMaxZoomClampReset());

SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(com.example.common_ui.R.id.map);
mapFragment.getMapAsync(this);
applyInsets(binding.mapContainer);
}

@Override
Expand All @@ -93,12 +102,36 @@ protected void onResume() {
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
map.setOnCameraMoveListener(this);
map.setOnCameraIdleListener(this);
updateCameraPosition();
}

@Override
public void onCameraMove() {
updateCameraPosition();
}

@Override
public void onCameraIdle() {
binding.cameraText.setText(mMap.getCameraPosition().toString());
updateCameraPosition();
}

private void updateCameraPosition() {
if (mMap == null) return;
CameraPosition pos = mMap.getCameraPosition();
binding.cameraText.setText(getString(
com.example.common_ui.R.string.camera_position_format,
pos.target.latitude,
pos.target.longitude,
pos.zoom,
pos.tilt,
pos.bearing
));
}

private void updateZoomLabel(float min, float max) {
binding.zoomLabel.setText(getString(com.example.common_ui.R.string.zoom_bounds_label, min, max));
}

/**
Expand Down Expand Up @@ -126,53 +159,40 @@ private void onClampToAdelaide() {
if (!checkReady()) {
return;
}
binding.latlngClampToggleGroup.check(com.example.common_ui.R.id.clamp_latlng_adelaide);
mMap.setLatLngBoundsForCameraTarget(ADELAIDE_BOUNDS);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(ADELAIDE_CAMERA));
binding.clampStatusText.setText(getString(com.example.common_ui.R.string.latlng_clamp_status_adelaide));
}

private void onClampToPacific() {
if (!checkReady()) {
return;
}
binding.latlngClampToggleGroup.check(com.example.common_ui.R.id.clamp_latlng_pacific);
mMap.setLatLngBoundsForCameraTarget(PACIFIC);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(PACIFIC_CAMERA));
binding.clampStatusText.setText(getString(com.example.common_ui.R.string.latlng_clamp_status_pacific));
}

private void onLatLngClampReset() {
if (!checkReady()) {
return;
}
binding.latlngClampToggleGroup.clearChecked();
// Setting bounds to null removes any previously set bounds.
mMap.setLatLngBoundsForCameraTarget(null);
binding.clampStatusText.setText(getString(com.example.common_ui.R.string.latlng_clamp_status_none));
toast("LatLngBounds clamp reset.");
}

private void onSetMinZoomClamp() {
if (!checkReady()) {
return;
}
mMinZoom += ZOOM_DELTA;
// Constrains the minimum zoom level.
mMap.setMinZoomPreference(mMinZoom);
toast("Min zoom preference set to: " + mMinZoom);
}

private void onSetMaxZoomClamp() {
if (!checkReady()) {
return;
}
mMaxZoom -= ZOOM_DELTA;
// Constrains the maximum zoom level.
mMap.setMaxZoomPreference(mMaxZoom);
toast("Max zoom preference set to: " + mMaxZoom);
}

private void onMinMaxZoomClampReset() {
if (!checkReady()) {
return;
}
resetMinMaxZoom();
mMap.resetMinMaxZoomPreference();
binding.zoomRangeSlider.setValues(DEFAULT_MIN_ZOOM, DEFAULT_MAX_ZOOM);
updateZoomLabel(DEFAULT_MIN_ZOOM, DEFAULT_MAX_ZOOM);
if (mMap != null) {
mMap.resetMinMaxZoomPreference();
}
toast("Min/Max zoom preferences reset.");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.view.View;
import android.view.animation.Interpolator;
Expand Down Expand Up @@ -89,7 +90,8 @@ public void onMapReady(GoogleMap map) {
// Add a marker to the Opera House.
mMap.addMarker(new MarkerOptions().position(SOH).title("Sydney Opera House"));
// Add a camera idle listener.
mMap.setOnCameraIdleListener(() -> binding.messageText.setText("CameraChangeListener: " + mMap.getCameraPosition()));
mMap.setOnCameraIdleListener(() -> binding.messageText.setText(
getString(com.example.common_ui.R.string.camera_change_message, mMap.getCameraPosition())));
}

/**
Expand Down Expand Up @@ -147,7 +149,7 @@ public void setMorePadding(View view) {
public void animatePadding(
final int toLeft, final int toTop, final int toRight, final int toBottom) {

final Handler handler = new Handler();
final Handler handler = new Handler(Looper.getMainLooper());
final long start = SystemClock.uptimeMillis();
final long duration = 1000;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
Expand Down Expand Up @@ -86,7 +87,8 @@ public void onMapReady(GoogleMap map) {
mMap.setOnCameraIdleListener(new OnCameraIdleListener() {
@Override
public void onCameraIdle() {
mMessageView.setText("CameraChangeListener: " + mMap.getCameraPosition());
mMessageView.setText(getString(
com.example.common_ui.R.string.camera_change_message, mMap.getCameraPosition()));
}
});
}
Expand Down Expand Up @@ -146,7 +148,7 @@ public void setMorePadding(View view) {
public void animatePadding(
final int toLeft, final int toTop, final int toRight, final int toBottom) {

final Handler handler = new Handler();
final Handler handler = new Handler(Looper.getMainLooper());
final long start = SystemClock.uptimeMillis();
final long duration = 1000;

Expand Down
Loading