Create Dynamic base map gallery

Use case
Multi-use and/or international applications benefit from the ability to change a basemap’s style or localize the basemap.
How to use the sample
Press “Basemap” to display a gallery of all styles available in the basemap styles service. Select a style using the “Style” picker. Select a language or language strategy using the “Language” picker. Optionally selected a worldview using the “Worldview” picker. Disabled pickers indicate that the customization cannot be applied to the selected style.
How it works
- Call
BasemapStylesService.load()and wrap each returnedBasemapStyleInfoin aBasemapGalleryItemto populate theBasemapGallerytoolkit composable. - Tap the floating action button to open a popup containing the
BasemapGallery, and select a basemap style item. - Choose a language from the selected style’s
BasemapStyleInfo.languages, and optionally a worldview from itsBasemapStyleInfo.worldviews. - On “Done”, create
BasemapStyleParameterswithlanguageStrategy = BasemapStyleLanguageStrategy.Specific(Locale.forLanguageTag(languageCode))and, if chosen,worldview. - Create a
Basemapfrom the selectedBasemapStyleand theBasemapStyleParameters, and apply it withArcGISMap.setBasemap().
Relevant API
- basemapgallery
Additional information
This sample uses the basemapgallery Toolkit module to be able to implement a composable MapView.
Tags
basemapgallery map
Sample code
/* Copyright 2026 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.createdynamicbasemapgallery.components
import android.app.Applicationimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateListOfimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.lifecycle.AndroidViewModelimport androidx.lifecycle.viewModelScopeimport com.arcgismaps.geometry.Pointimport com.arcgismaps.geometry.SpatialReferenceimport com.arcgismaps.mapping.ArcGISMapimport com.arcgismaps.mapping.Basemapimport com.arcgismaps.mapping.BasemapStyleimport com.arcgismaps.mapping.BasemapStyleInfoimport com.arcgismaps.mapping.BasemapStyleLanguageInfoimport com.arcgismaps.mapping.BasemapStyleLanguageStrategyimport com.arcgismaps.mapping.BasemapStyleParametersimport com.arcgismaps.mapping.BasemapStylesServiceimport com.arcgismaps.mapping.Viewpointimport com.arcgismaps.mapping.Worldviewimport com.arcgismaps.toolkit.basemapgallery.BasemapGalleryItemimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModelimport kotlinx.coroutines.launchimport java.util.Locale
class CreateDynamicBasemapGalleryViewModel(app: Application) : AndroidViewModel(app) {
// Map initialized with an imagery basemap to match the Swift sample val arcGISMap by mutableStateOf( ArcGISMap(BasemapStyle.ArcGISImagery).apply { // Initial viewpoint centered near Los Angeles, CA at a scale of 1:1,000,000 initialViewpoint = Viewpoint( center = Point( x = -118.4, y = 33.7, spatialReference = SpatialReference.wgs84() ), scale = 1e6 ) } )
// Items displayed by the Basemap Gallery val basemapGalleryItems = mutableStateListOf<BasemapGalleryItem>()
// The style info of the currently selected basemap gallery item var selectedBasemapStyleInfo by mutableStateOf<BasemapStyleInfo?>(null) private set
// The language currently applied to the selected basemap, if any var selectedLanguage by mutableStateOf<BasemapStyleLanguageInfo?>(null) private set
// The worldview currently applied to the selected basemap, if any var selectedWorldview by mutableStateOf<Worldview?>(null) private set
// Create a message dialog view model for handling error messages val messageDialogVM = MessageDialogViewModel()
init { viewModelScope.launch { arcGISMap.load().onFailure { messageDialogVM.showMessageDialog(it) }
// Load available basemap styles from the BasemapStyles service and create gallery items val service = BasemapStylesService() service.load().onSuccess { val stylesInfo = service.info?.stylesInfo if (!stylesInfo.isNullOrEmpty()) { stylesInfo.forEach { basemapStyleInfo -> basemapGalleryItems.add(BasemapGalleryItem(basemapStyleInfo)) } selectedBasemapStyleInfo = stylesInfo.firstOrNull { it.style == BasemapStyle.ArcGISImagery } } else { messageDialogVM.showMessageDialog( title = "No basemap styles available", description = "BasemapStylesService returned no styles." ) } }.onFailure { error -> messageDialogVM.showMessageDialog( title = "Failed to load basemap styles", description = error.message.toString() ) } } }
fun onDoneClicked( item: BasemapGalleryItem?, languageInfo: BasemapStyleLanguageInfo?, worldview: Worldview? ) { // Nothing was selected, so there is nothing to update if (item == null) return
when (val tag = item.tag) { is BasemapStyleInfo -> { selectedBasemapStyleInfo = tag selectedLanguage = languageInfo selectedWorldview = worldview updateBasemap(basemapStyleInfo = tag, languageInfo = languageInfo, worldview = worldview) } else -> { messageDialogVM.showMessageDialog( title = "Unsupported item type", description = "The selected gallery item is not a BasemapStyleInfo." ) } } }
// Update the BaseMap with selected style, language and worldview private fun updateBasemap( basemapStyleInfo: BasemapStyleInfo, languageInfo: BasemapStyleLanguageInfo? = null, worldview: Worldview? = null ) { val parameters = BasemapStyleParameters().apply { if (languageInfo != null) { languageStrategy = BasemapStyleLanguageStrategy.Specific(Locale.forLanguageTag(languageInfo.languageCode)) } if (worldview != null) { this.worldview = worldview } } arcGISMap.setBasemap( basemap = Basemap(basemapStyle = basemapStyleInfo.style, basemapStyleParameters = parameters) ) }}/* Copyright 2026 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.createdynamicbasemapgallery
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.activity.enableEdgeToEdgeimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Surfaceimport com.esri.arcgismaps.sample.sampleslib.theme.SampleAppThemeimport com.esri.arcgismaps.sample.createdynamicbasemapgallery.screens.CreateDynamicBasemapGalleryScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { SampleAppTheme { Surface(color = MaterialTheme.colorScheme.background) { CreateDynamicBasemapGalleryScreen() } } } }}/* Copyright 2026 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.createdynamicbasemapgallery.screens
import android.graphics.drawable.BitmapDrawableimport androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.aspectRatioimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.lazy.grid.GridCellsimport androidx.compose.foundation.lazy.grid.LazyVerticalGridimport androidx.compose.foundation.lazy.grid.itemsimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.material.icons.Iconsimport androidx.compose.material.icons.filled.Mapimport androidx.compose.material3.Buttonimport androidx.compose.material3.FloatingActionButtonimport androidx.compose.material3.Iconimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.OutlinedButtonimport androidx.compose.material3.Scaffoldimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.produceStateimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clipimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.graphics.asImageBitmapimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.res.stringResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.unit.dpimport androidx.compose.ui.window.Dialogimport androidx.lifecycle.viewmodel.compose.viewModelimport com.arcgismaps.mapping.BasemapStyleInfoimport com.arcgismaps.toolkit.basemapgallery.BasemapGalleryItemimport com.arcgismaps.toolkit.geoviewcompose.MapViewimport com.esri.arcgismaps.sample.createdynamicbasemapgallery.Rimport com.esri.arcgismaps.sample.createdynamicbasemapgallery.components.CreateDynamicBasemapGalleryViewModelimport com.esri.arcgismaps.sample.sampleslib.components.DropDownMenuBoximport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogimport com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar
/** * Main screen layout for the sample app */@Composablefun CreateDynamicBasemapGalleryScreen() { val mapViewModel: CreateDynamicBasemapGalleryViewModel = viewModel()
// Controls whether the basemap gallery popup is shown var showBasemapGallery by remember { mutableStateOf(false) }
Scaffold( topBar = { SampleTopAppBar(title = stringResource(R.string.create_dynamic_basemap_gallery_app_name)) } ) { padding -> Box( modifier = Modifier .fillMaxSize() .padding(padding) ) { MapView( modifier = Modifier.fillMaxSize(), arcGISMap = mapViewModel.arcGISMap )
FloatingActionButton( onClick = { showBasemapGallery = true }, modifier = Modifier .align(Alignment.TopStart) .padding(16.dp) ) { Icon( imageVector = Icons.Filled.Map, contentDescription = "Show basemap gallery" ) } }
// Display menu if (showBasemapGallery) { // Default the pending selection to the actual gallery item for the basemap style // currently applied to the map, so it can be highlighted when the gallery opens. // Keyed on selectedBasemapStyleInfo so it re-syncs once the basemap style service // finishes loading, in case the gallery was opened before that completed. var pendingItem by remember(mapViewModel.selectedBasemapStyleInfo) { mutableStateOf( mapViewModel.basemapGalleryItems.firstOrNull { item -> (item.tag as? BasemapStyleInfo)?.style == mapViewModel.selectedBasemapStyleInfo?.style } ) } var pendingLanguage by remember(mapViewModel.selectedLanguage) { mutableStateOf(mapViewModel.selectedLanguage) } var pendingWorldview by remember(mapViewModel.selectedWorldview) { mutableStateOf(mapViewModel.selectedWorldview) }
Dialog(onDismissRequest = { showBasemapGallery = false }) { Column( modifier = Modifier .width(320.dp) .height(520.dp) .clip(RoundedCornerShape(12.dp)) .background(MaterialTheme.colorScheme.background) .padding(12.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(4.dp) ) { Text( text = "Basemap Gallery", style = MaterialTheme.typography.titleLarge, modifier = Modifier.padding(bottom = 8.dp) ) Text( text = "Choose a basemap style to change the map's basemap.", style = MaterialTheme.typography.labelSmall, modifier = Modifier.padding(bottom = 8.dp) )
LazyVerticalGrid( columns = GridCells.Adaptive(96.dp), modifier = Modifier .fillMaxWidth() .weight(1f), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp) ) { items(mapViewModel.basemapGalleryItems) { item -> BasemapGalleryItemCard( item = item, selected = item === pendingItem, onClick = { pendingItem = item pendingLanguage = null pendingWorldview = null } ) } }
// Lists the languages supported by the pending basemap style selection val languageOptions = (pendingItem?.tag as? BasemapStyleInfo)?.languages.orEmpty() DropDownMenuBox( modifier = Modifier.fillMaxWidth(), textFieldValue = pendingLanguage?.displayName ?: "Default", textFieldLabel = "Language", dropDownItemList = languageOptions.map { it.displayName }, onIndexSelected = { index -> pendingLanguage = languageOptions[index] } )
// Lists the worldviews supported by the pending basemap style selection val worldviewOptions = (pendingItem?.tag as? BasemapStyleInfo)?.worldviews.orEmpty() DropDownMenuBox( modifier = Modifier.fillMaxWidth(), textFieldValue = pendingWorldview?.displayName ?: "Default", textFieldLabel = "Worldview", dropDownItemList = worldviewOptions.map { it.displayName }, onIndexSelected = { index -> pendingWorldview = worldviewOptions[index] } )
Row( modifier = Modifier .fillMaxWidth() .padding(top = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End) ) { OutlinedButton(onClick = { showBasemapGallery = false }) { Text("Cancel") } Button( onClick = { mapViewModel.onDoneClicked( item = pendingItem, languageInfo = pendingLanguage, worldview = pendingWorldview ) showBasemapGallery = false } ) { Text("Done") } } } } }
// Display a dialog if the sample encounters an error mapViewModel.messageDialogVM.apply { if (dialogStatus) { MessageDialog( title = messageTitle, description = messageDescription, onDismissRequest = ::dismissDialog ) } } }}
// Base Map Gallery card for each Style@Composableprivate fun BasemapGalleryItemCard( item: BasemapGalleryItem, selected: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier) { val basemapStyleInfo = item.tag as? BasemapStyleInfo val thumbnail by produceState<BitmapDrawable?>(initialValue = null, key1 = basemapStyleInfo) { value = basemapStyleInfo?.thumbnail?.let { thumbnailImage -> thumbnailImage.load() thumbnailImage.image } }
Column( modifier = modifier .clip(RoundedCornerShape(8.dp)) .background(if (selected) MaterialTheme.colorScheme.primaryContainer else Color.Transparent) .clickable(onClick = onClick) .padding(4.dp), horizontalAlignment = Alignment.CenterHorizontally ) { Box( modifier = Modifier .fillMaxWidth() .aspectRatio(1f) .clip(RoundedCornerShape(6.dp)) .background(MaterialTheme.colorScheme.surfaceVariant) ) { thumbnail?.let { drawable -> Image( bitmap = drawable.bitmap.asImageBitmap(), contentDescription = item.title, contentScale = ContentScale.Crop, modifier = Modifier.fillMaxSize() ) } } Text( text = item.title, style = MaterialTheme.typography.labelSmall, color = if (selected) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.onBackground, textAlign = TextAlign.Center, maxLines = 2, modifier = Modifier.padding(top = 4.dp) ) }}