@@ -13,6 +13,7 @@ import androidx.activity.compose.setContent
1313import androidx.activity.result.ActivityResult
1414import androidx.activity.result.contract.ActivityResultContracts
1515import androidx.appcompat.app.AppCompatActivity
16+ import androidx.appcompat.app.AppCompatDelegate
1617import androidx.compose.foundation.Image
1718import androidx.compose.foundation.layout.Arrangement
1819import androidx.compose.foundation.layout.Box
@@ -153,9 +154,32 @@ class MainActivity : AppCompatActivity() {
153154 }
154155}
155156
157+ /* *
158+ * Toggles between Light and Dark mode for the sample app.
159+ */
160+ private fun toggleNightMode (context : Context ) {
161+ val currentNightMode =
162+ context.resources.configuration.uiMode and android.content.res.Configuration .UI_MODE_NIGHT_MASK
163+
164+ val newMode =
165+ when (currentNightMode) {
166+ android.content.res.Configuration .UI_MODE_NIGHT_YES -> AppCompatDelegate .MODE_NIGHT_NO
167+ else -> AppCompatDelegate .MODE_NIGHT_YES
168+ }
169+
170+ AppCompatDelegate .setDefaultNightMode(newMode)
171+ }
172+
173+ private fun isNightMode (context : Context ): Boolean {
174+ val currentNightMode =
175+ context.resources.configuration.uiMode and android.content.res.Configuration .UI_MODE_NIGHT_MASK
176+ return currentNightMode == android.content.res.Configuration .UI_MODE_NIGHT_YES
177+ }
178+
156179private fun onLaunchMapPickerClicked (context : Context ) {
157180 val activity = context as MainActivity
158- val locationPickerIntent =
181+
182+ val builder =
159183 LocationPickerActivity
160184 .Builder (activity)
161185 .withLocation(DEMO_LATITUDE , DEMO_LONGITUDE )
@@ -175,8 +199,15 @@ private fun onLaunchMapPickerClicked(context: Context) {
175199 // .withVoiceSearchHidden()
176200 .withUnnamedRoadHidden()
177201 .withSolidBottomColor()
178- // .withSearchBarHidden()
179- .build()
202+ // .withSearchBarHidden()
203+
204+ // Optional: use a dark Google Maps style when night mode is enabled
205+ // (requires you to add app/src/main/res/raw/map_style_night.json)
206+ if (isNightMode(context)) {
207+ builder.withMapStyle(R .raw.map_style_night)
208+ }
209+
210+ val locationPickerIntent = builder.build()
180211
181212 // this is optional if you want to return RESULT_OK if you don't set the
182213 // latitude/longitude and click back button
@@ -231,12 +262,19 @@ private fun onMapPoisClicked(context: Context) {
231262
232263private fun onMapWithStylesClicked (context : Context ) {
233264 val activity = context as MainActivity
234- val locationPickerIntent =
265+
266+ val builder =
235267 LocationPickerActivity
236268 .Builder (activity)
237269 .withLocation(DEMO_LATITUDE , DEMO_LONGITUDE )
238- .withMapStyle(R .raw.map_style_retro)
239- .build()
270+
271+ if (isNightMode(context)) {
272+ builder.withMapStyle(R .raw.map_style_night)
273+ } else {
274+ builder.withMapStyle(R .raw.map_style_retro)
275+ }
276+
277+ val locationPickerIntent = builder.build()
240278 activity.mapPoisActivityResultLauncher.launch(locationPickerIntent)
241279}
242280
@@ -260,6 +298,24 @@ fun MainView() {
260298 painter = painterResource(id = R .mipmap.leku_img_logo),
261299 contentDescription = null ,
262300 )
301+
302+ Button (
303+ colors =
304+ ButtonDefaults .buttonColors(
305+ backgroundColor = buttonColor,
306+ contentColor = Color .White ,
307+ ),
308+ onClick = { toggleNightMode(context) },
309+ ) {
310+ Text (
311+ " Toggle Dark / Light" ,
312+ Modifier
313+ .padding(8 .dp)
314+ .fillMaxWidth(),
315+ textAlign = TextAlign .Center ,
316+ )
317+ }
318+
263319 Button (
264320 colors =
265321 ButtonDefaults .buttonColors(
0 commit comments