Skip to content

Commit 683b21d

Browse files
authored
fix: support devices with cutouts for DataDrivenDatasetStylingActivity.java demo (#1979)
* fix: support devices with cutouts for DataDrivenDatasetStylingActivity.java demo * fix: remove unnecessary 'res/' from paths in README.md file * feat(apidemos): enhance DataDrivenBoundariesActivity UI and boundary selection This commit enhances the DataDrivenBoundariesActivity demo with UI and functionality improvements focusing on boundary type selection and visual enhancements. - Implements Material Design theming and UI elements. - Adds boundary type selection via PopupMenu (Locality, Admin Area, Country). - Refactors styling and implements persistent country selection. - Handles system UI insets for improved display. These changes improve the demo's user experience and code structure, better showcasing data-driven boundary styling. * feat(apidemos): configure dataset-specific zoom levels for datasets demo This commit introduces dataset-specific zoom levels to the DataDrivenDatasetStylingActivity, enhancing the user experience when switching between datasets. - Adds a `zoomLevel` field to the `DataSet` class to store the desired zoom level for each dataset. - Updates the `dataSets` array to include appropriate zoom levels for Boulder, New York, and Kyoto datasets. - Modifies the `centerMapOnLocation` method to accept a `zoomLevel` parameter, allowing it to be dynamically set. - Updates the `switchDataSet` method to utilize the `zoomLevel` from the selected `DataSet` when centering the map, ensuring the map zooms to the optimal level for each dataset. - Removes the previously hardcoded `ZOOM_LEVEL` constant, as the zoom level is now dataset-dependent. These changes ensure that when a user selects a dataset, the map automatically zooms to a relevant level for that specific dataset, improving clarity and usability of the demo. Also adds missing copyright header.
1 parent 45d85ac commit 683b21d

File tree

14 files changed

+474
-223
lines changed

14 files changed

+474
-223
lines changed

ApiDemos/java/app/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ dependencies {
5858
implementation(libs.appcompat)
5959
implementation(libs.recyclerview)
6060
implementation(libs.volley)
61-
implementation(platform(libs.kotlinBom))
6261
implementation(libs.playServicesMaps)
62+
implementation(libs.material)
63+
implementation(libs.activity)
6364

6465
// Tests
6566
testImplementation(libs.junit)

ApiDemos/java/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ limitations under the License.
9292
android:label="@string/circle_demo_label" />
9393
<activity
9494
android:name=".DataDrivenBoundariesActivity"
95+
android:theme="@style/MaterialAppTheme"
9596
android:exported="true"
9697
android:label="@string/data_driven_boundaries_label" />
9798
<activity
9899
android:name=".DataDrivenDatasetStylingActivity"
100+
android:theme="@style/MaterialAppTheme"
99101
android:exported="true"
100102
android:label="@string/data_driven_styling_label" />
101103
<activity

ApiDemos/java/app/src/main/java/com/example/mapdemo/ApiDemoApplication.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class ApiDemoApplication extends Application {
4141
@Override
4242
public void onCreate() {
4343
super.onCreate();
44-
Log.d(TAG, "onCreate called");
4544
checkApiKey();
4645
}
4746

ApiDemos/java/app/src/main/java/com/example/mapdemo/DataDrivenBoundariesActivity.java

Lines changed: 221 additions & 78 deletions
Large diffs are not rendered by default.

ApiDemos/java/app/src/main/java/com/example/mapdemo/DataDrivenDatasetStylingActivity.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616
import android.graphics.Color;
1717
import android.os.Bundle;
1818
import android.util.Log;
19+
import android.view.View;
1920
import android.widget.Button;
2021
import android.widget.Toast;
2122

23+
import androidx.activity.EdgeToEdge;
2224
import androidx.annotation.NonNull;
2325
import androidx.appcompat.app.AppCompatActivity;
2426
import androidx.core.graphics.ColorUtils;
27+
import androidx.core.graphics.Insets;
28+
import androidx.core.view.ViewCompat;
29+
import androidx.core.view.WindowInsetsCompat;
2530

2631
import com.google.android.gms.maps.CameraUpdateFactory;
2732
import com.google.android.gms.maps.GoogleMap;
@@ -54,6 +59,7 @@ private record DataSet(
5459
String label,
5560
String datasetId,
5661
LatLng location,
62+
float zoomLevel,
5763
DataDrivenDatasetStylingActivity.DataSet.StylingCallback callback) {
5864
public interface StylingCallback {
5965
void styleDatasetLayer();
@@ -81,9 +87,9 @@ public interface StylingCallback {
8187
* Note: We have use the secrets plugin to allow us to configure the Dataset IDs in our secrets.properties file.
8288
*/
8389
private final DataSet[] dataSets = new DataSet[] {
84-
new DataSet("Boulder", BuildConfig.BOULDER_DATASET_ID, new LatLng(40.0150, -105.2705), this::styleBoulderDatasetLayer),
85-
new DataSet("New York", BuildConfig.NEW_YORK_DATASET_ID, new LatLng(40.786244, -73.962684), this::styleNYCDatasetLayer),
86-
new DataSet("Kyoto", BuildConfig.KYOTO_DATASET_ID, new LatLng(35.005081, 135.764385), this::styleKyotoDatasetsLayer),
90+
new DataSet("Boulder", BuildConfig.BOULDER_DATASET_ID, new LatLng(40.0150, -105.2705), 11f, this::styleBoulderDatasetLayer),
91+
new DataSet("New York", BuildConfig.NEW_YORK_DATASET_ID, new LatLng(40.786244, -73.962684), 14f, this::styleNYCDatasetLayer),
92+
new DataSet("Kyoto", BuildConfig.KYOTO_DATASET_ID, new LatLng(35.005081, 135.764385), 13.5f, this::styleKyotoDatasetsLayer),
8793
};
8894

8995
private DataSet findDataSetByLabel(String label) {
@@ -95,7 +101,6 @@ private DataSet findDataSetByLabel(String label) {
95101
return null; // Return null if no match is found
96102
}
97103

98-
private static final float ZOOM_LEVEL = 13.5f;
99104
private static final String TAG = DataDrivenDatasetStylingActivity.class.getName();
100105
private static FeatureLayer datasetLayer = null;
101106
private GoogleMap map;
@@ -104,8 +109,16 @@ private DataSet findDataSetByLabel(String label) {
104109
@Override
105110
protected void onCreate(Bundle savedInstanceState) {
106111
super.onCreate(savedInstanceState);
112+
EdgeToEdge.enable(this);
107113
setContentView(R.layout.data_driven_styling_demo);
108114

115+
// [START_EXCLUDE silent]
116+
if (getString(R.string.map_id).equals("DEMO_MAP_ID")) {
117+
// This demo will not work if the map id is not set.
118+
Toast.makeText(this, "Map ID is not set. See README for instructions.", Toast.LENGTH_LONG).show();
119+
}
120+
// [END_EXCLUDE]
121+
109122
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
110123
if (mapFragment != null) {
111124
mapFragment.getMapAsync(this);
@@ -115,6 +128,18 @@ protected void onCreate(Bundle savedInstanceState) {
115128
for (int buttonId : buttonIds) {
116129
findViewById(buttonId).setOnClickListener(view -> switchDataSet(((Button) view).getText().toString()));
117130
}
131+
132+
applyInsets(findViewById(R.id.map_container));
133+
}
134+
135+
private static void applyInsets(View container) {
136+
ViewCompat.setOnApplyWindowInsetsListener(container,
137+
(view, insets) -> {
138+
Insets innerPadding = insets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
139+
view.setPadding(innerPadding.left, innerPadding.top, innerPadding.right, innerPadding.bottom);
140+
return insets;
141+
}
142+
);
118143
}
119144

120145
/**
@@ -142,7 +167,7 @@ private void switchDataSet(String label) {
142167
.build()
143168
);
144169
dataSet.callback.styleDatasetLayer();
145-
centerMapOnLocation(dataSet.location());
170+
centerMapOnLocation(dataSet.location(), dataSet.zoomLevel());
146171
}
147172
}
148173

@@ -152,6 +177,13 @@ public void onMapReady(@NonNull GoogleMap googleMap) {
152177

153178
MapCapabilities capabilities = map.getMapCapabilities();
154179
Log.d(TAG, "Data-driven Styling is available: " + capabilities.isDataDrivenStylingAvailable());
180+
if (!capabilities.isDataDrivenStylingAvailable()) {
181+
Toast.makeText(
182+
this,
183+
"Data-driven Styling is not available. See README.md for instructions.",
184+
Toast.LENGTH_LONG
185+
).show();
186+
}
155187

156188
// Switch to the default dataset which must happen before adding the feature click listener
157189
switchDataSet("Boulder");
@@ -270,8 +302,8 @@ private void styleBoulderDatasetLayer() {
270302
// Create the style factory function.
271303
FeatureLayer.StyleFactory styleFactory = feature -> {
272304
// Set default colors to yellow and point radius to 8.
273-
int fillColor = Color.GREEN;
274-
int strokeColor = Color.YELLOW;
305+
int fillColor;
306+
int strokeColor;
275307
float pointRadius = 8F;
276308
float strokeWidth = 3F;
277309

@@ -323,8 +355,8 @@ private void styleBoulderDatasetLayer() {
323355
}
324356

325357

326-
private void centerMapOnLocation(LatLng location) {
327-
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, ZOOM_LEVEL));
358+
private void centerMapOnLocation(LatLng location, float zoomLevel) {
359+
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, zoomLevel));
328360
}
329361

330362
@Override
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
18+
19+
<path android:fillColor="@android:color/white" android:pathData="M7,10l5,5 5,-5z"/>
20+
21+
</vector>

ApiDemos/java/app/src/main/res/layout/data_driven_boundaries_demo.xml

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,38 @@
1414
limitations under the License.
1515
-->
1616

17-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:app="http://schemas.android.com/apk/res-auto"
1819
xmlns:map="http://schemas.android.com/apk/res-auto"
20+
android:id="@+id/map_container"
1921
android:layout_width="match_parent"
20-
android:layout_height="match_parent">
21-
<!-- Map Fragment -->
22+
android:layout_height="match_parent"
23+
android:fitsSystemWindows="true"
24+
>
25+
26+
<com.google.android.material.appbar.MaterialToolbar
27+
android:id="@+id/top_bar"
28+
android:layout_width="0dp"
29+
android:layout_height="?attr/actionBarSize"
30+
app:layout_constraintEnd_toEndOf="parent"
31+
app:layout_constraintStart_toStartOf="parent"
32+
app:layout_constraintTop_toTopOf="parent"
33+
app:title="@string/data_driven_boundaries_label"
34+
app:titleTextColor="?attr/colorOnPrimary"
35+
style="@style/Widget.MaterialComponents.Toolbar.Primary"
36+
/>
37+
2238
<fragment
2339
android:id="@+id/map"
24-
android:layout_width="match_parent"
25-
android:layout_height="match_parent"
40+
android:layout_width="0dp"
41+
android:layout_height="0dp"
2642
map:backgroundColor="#fff0b2dd"
2743
map:mapId="@string/map_id"
28-
class="com.google.android.gms.maps.SupportMapFragment" />
44+
class="com.google.android.gms.maps.SupportMapFragment"
45+
app:layout_constraintBottom_toBottomOf="parent"
46+
app:layout_constraintEnd_toEndOf="parent"
47+
app:layout_constraintStart_toStartOf="parent"
48+
app:layout_constraintTop_toBottomOf="@+id/top_bar" />
2949

3050
<!-- Buttons to center the map -->
3151
<LinearLayout
@@ -34,20 +54,33 @@
3454
android:layout_alignParentTop="true"
3555
android:layout_centerHorizontal="true"
3656
android:orientation="horizontal"
37-
android:padding="16dp">
38-
39-
<Button
57+
android:padding="16dp"
58+
app:layout_constraintEnd_toEndOf="parent"
59+
app:layout_constraintStart_toStartOf="parent"
60+
app:layout_constraintTop_toBottomOf="@+id/top_bar"
61+
>
62+
<com.google.android.material.button.MaterialButton
4063
android:id="@+id/button_hawaii"
4164
android:layout_width="wrap_content"
4265
android:layout_height="wrap_content"
43-
android:text="Hawaii" />
66+
android:layout_margin="4dp"
67+
android:text="@string/hawaii" />
4468

45-
<Button
69+
<com.google.android.material.button.MaterialButton
4670
android:id="@+id/button_us"
4771
android:layout_width="wrap_content"
4872
android:layout_height="wrap_content"
49-
android:text="US" />
50-
</LinearLayout>
73+
android:layout_margin="4dp"
74+
android:text="@string/united_states" />
5175

76+
<com.google.android.material.button.MaterialButton
77+
android:id="@+id/button_feature_type"
78+
android:layout_width="wrap_content"
79+
android:layout_height="wrap_content"
80+
android:drawableEnd="@drawable/baseline_arrow_drop_down_24"
81+
android:layout_margin="4dp"
82+
android:text="@string/boundary_type" />
83+
84+
</LinearLayout>
5285

53-
</RelativeLayout>
86+
</androidx.constraintlayout.widget.ConstraintLayout>

ApiDemos/java/app/src/main/res/layout/data_driven_styling_demo.xml

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,68 @@
1414
limitations under the License.
1515
-->
1616

17-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:app="http://schemas.android.com/apk/res-auto"
1819
xmlns:map="http://schemas.android.com/apk/res-auto"
20+
android:id="@+id/map_container"
1921
android:layout_width="match_parent"
2022
android:layout_height="match_parent">
21-
<!-- Map Fragment -->
23+
24+
<com.google.android.material.appbar.MaterialToolbar
25+
android:id="@+id/top_bar"
26+
android:layout_width="0dp"
27+
android:layout_height="?attr/actionBarSize"
28+
app:layout_constraintEnd_toEndOf="parent"
29+
app:layout_constraintStart_toStartOf="parent"
30+
app:layout_constraintTop_toTopOf="parent"
31+
app:title="@string/data_driven_styling_label"
32+
app:titleTextColor="?attr/colorOnPrimary"
33+
style="@style/Widget.MaterialComponents.Toolbar.Primary"
34+
/>
35+
2236
<fragment
2337
android:id="@+id/map"
24-
android:layout_width="match_parent"
25-
android:layout_height="match_parent"
38+
android:layout_width="0dp"
39+
android:layout_height="0dp"
2640
map:backgroundColor="#fff0b2dd"
2741
map:mapId="@string/map_id"
28-
class="com.google.android.gms.maps.SupportMapFragment" />
42+
class="com.google.android.gms.maps.SupportMapFragment"
43+
app:layout_constraintBottom_toBottomOf="parent"
44+
app:layout_constraintEnd_toEndOf="parent"
45+
app:layout_constraintStart_toStartOf="parent"
46+
app:layout_constraintTop_toBottomOf="@+id/top_bar" />
2947

30-
<!-- Buttons to center the map -->
3148
<LinearLayout
49+
android:id="@+id/button_bar"
3250
android:layout_width="wrap_content"
3351
android:layout_height="wrap_content"
34-
android:layout_alignParentTop="true"
35-
android:layout_centerHorizontal="true"
3652
android:orientation="horizontal"
37-
android:padding="16dp">
53+
android:padding="16dp"
54+
app:layout_constraintEnd_toEndOf="parent"
55+
app:layout_constraintStart_toStartOf="parent"
56+
app:layout_constraintTop_toBottomOf="@+id/top_bar">
3857

39-
<Button
58+
<com.google.android.material.button.MaterialButton
4059
android:id="@+id/button_boulder"
4160
android:layout_width="wrap_content"
4261
android:layout_height="wrap_content"
43-
android:text="Boulder" />
62+
android:layout_margin="4dp"
63+
android:text="@string/boulder" />
4464

45-
<Button
65+
<com.google.android.material.button.MaterialButton
4666
android:id="@+id/button_ny"
4767
android:layout_width="wrap_content"
4868
android:layout_height="wrap_content"
49-
android:text="New York" />
69+
android:layout_margin="4dp"
70+
android:text="@string/new_york" />
5071

51-
<Button
72+
<com.google.android.material.button.MaterialButton
5273
android:id="@+id/button_kyoto"
5374
android:layout_width="wrap_content"
5475
android:layout_height="wrap_content"
55-
android:text="Kyoto" />
56-
</LinearLayout>
76+
android:layout_margin="4dp"
77+
android:text="@string/kyoto" />
5778

79+
</LinearLayout>
5880

59-
</RelativeLayout>
81+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)