Skip to content

Commit 4836b9b

Browse files
committed
feat: Implement edge-to-edge UI
This commit implements edge-to-edge UI for the demo application. Changes include: - Updating the app theme to be edge-to-edge. - Applying window insets to the base and main activities. - Adding a separate styles.xml for API 27+ to handle display cutouts.
1 parent 3f461e6 commit 4836b9b

5 files changed

Lines changed: 112 additions & 5 deletions

File tree

demo/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ dependencies {
6565
implementation(libs.kotlinx.coroutines.android)
6666
implementation(libs.kotlinx.coroutines.core)
6767
implementation(libs.material)
68+
implementation(libs.core.ktx)
6869

6970
testImplementation(libs.junit)
7071
testImplementation(libs.truth)

demo/src/main/java/com/google/maps/android/utils/demo/BaseDemoActivity.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.maps.android.utils.demo.ApiKeyValidatorKt.hasMapsApiKey;
2020

2121
import android.os.Bundle;
22+
import android.view.View;
2223
import android.widget.Toast;
2324

2425
import androidx.annotation.NonNull;
@@ -30,6 +31,11 @@
3031

3132
import java.util.Objects;
3233

34+
import androidx.core.graphics.Insets;
35+
import androidx.core.view.ViewCompat;
36+
import androidx.core.view.WindowCompat;
37+
import androidx.core.view.WindowInsetsCompat;
38+
3339
public abstract class BaseDemoActivity extends FragmentActivity implements OnMapReadyCallback {
3440
private GoogleMap mMap;
3541
private boolean mIsRestore;
@@ -49,6 +55,30 @@ public void onCreate(Bundle savedInstanceState) {
4955

5056
mIsRestore = savedInstanceState != null;
5157
setContentView(getLayoutId());
58+
// This tells the system that the app will handle drawing behind the system bars.
59+
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
60+
61+
// This is the root view of my layout.
62+
// Make sure to replace R.id.root_layout with the actual ID of your root view.
63+
final View rootView = findViewById(android.R.id.content);
64+
65+
// Add a listener to handle window insets.
66+
ViewCompat.setOnApplyWindowInsetsListener(rootView, (view, windowInsets) -> {
67+
final Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
68+
69+
// Apply the insets as padding to the view.
70+
// This will push the content down from behind the status bar and up from
71+
// behind the navigation bar.
72+
view.setPadding(
73+
insets.left,
74+
insets.top,
75+
insets.right,
76+
insets.bottom
77+
);
78+
79+
// Return CONSUMED to signal that we've handled the insets.
80+
return WindowInsetsCompat.CONSUMED;
81+
});
5282
setUpMap();
5383
}
5484

demo/src/main/java/com/google/maps/android/utils/demo/MainActivity.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626
import androidx.appcompat.app.AppCompatActivity;
2727

28+
import androidx.core.graphics.Insets;
29+
import androidx.core.view.ViewCompat;
30+
import androidx.core.view.WindowCompat;
31+
import androidx.core.view.WindowInsetsCompat;
32+
2833
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
2934
private ViewGroup mListView;
3035

@@ -34,6 +39,31 @@ protected void onCreate(Bundle savedInstanceState) {
3439

3540
setContentView(R.layout.main);
3641

42+
// This tells the system that the app will handle drawing behind the system bars.
43+
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
44+
45+
// This is the root view of my layout.
46+
// Make sure to replace R.id.root_layout with the actual ID of your root view.
47+
final View rootView = findViewById(android.R.id.content);
48+
49+
// Add a listener to handle window insets.
50+
ViewCompat.setOnApplyWindowInsetsListener(rootView, (view, windowInsets) -> {
51+
final Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
52+
53+
// Apply the insets as padding to the view.
54+
// This will push the content down from behind the status bar and up from
55+
// behind the navigation bar.
56+
view.setPadding(
57+
insets.left,
58+
insets.top,
59+
insets.right,
60+
insets.bottom
61+
);
62+
63+
// Return CONSUMED to signal that we've handled the insets.
64+
return WindowInsetsCompat.CONSUMED;
65+
});
66+
3767
mListView = findViewById(R.id.list);
3868

3969
addDemo("Clustering", ClusteringDemoActivity.class);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!--
2+
~ Copyright 2020 Google Inc.
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+
<resources>
17+
18+
<!-- Base application theme. -->
19+
<style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
20+
<!-- Other theme attributes -->
21+
22+
<!-- Make status and navigation bars transparent -->
23+
<item name="android:statusBarColor">@android:color/transparent</item>
24+
<item name="android:navigationBarColor">@android:color/transparent</item>
25+
26+
<!--
27+
Required for edge-to-edge display. This allows the app to draw behind
28+
the system bars.
29+
-->
30+
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
31+
32+
<!--
33+
On API 27+, this allows the app to render in the display cutout area.
34+
'shortEdges' is a good default.
35+
-->
36+
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
37+
</style>
38+
39+
</resources>

demo/src/main/res/values/styles.xml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616
<resources>
1717

1818
<!-- Base application theme. -->
19-
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
20-
<!-- Customize your theme here. -->
21-
<item name="colorPrimary">@color/colorPrimary</item>
22-
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
23-
<item name="colorAccent">@color/colorAccent</item>
19+
<style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
20+
<!-- Other theme attributes -->
21+
22+
<!-- Make status and navigation bars transparent -->
23+
<item name="android:statusBarColor">@android:color/transparent</item>
24+
<item name="android:navigationBarColor">@android:color/transparent</item>
25+
26+
<!--
27+
Required for edge-to-edge display. This allows the app to draw behind
28+
the system bars.
29+
-->
30+
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
2431
</style>
2532

2633
</resources>

0 commit comments

Comments
 (0)