Search code examples
androidkotlinandroid-jetpack-composeandroid-jetpack-compose-material3

Numerals appear as tab names in NavigationSuiteScaffoldLayout instead of string resources


Why numerals are shown instead of string resources for the tabs in the NavigationSuiteScaffoldLayout even though they are declared as string resources?

MainActivity.xml

enum class AppDestinations(
    @StringRes val label: Int,
    val icon: ImageVector,
    @StringRes val contentDescription: Int
) {
    HOME(R.string.home, Icons.Default.Home, R.string.home),
    FAVORITES(R.string.favorites, Icons.Default.Favorite, R.string.favorites),
    SHOPPING(R.string.shopping, Icons.Default.ShoppingCart, R.string.shopping),
    PROFILE(R.string.profile, Icons.Default.AccountBox, R.string.profile),
}

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyAdaptiveApplicationTheme {
                    var selectedItem by remember { mutableIntStateOf(0) }
                    val navItems = listOf("Songs", "Artists", "Playlists")
                    val navSuiteType =
                        NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(currentWindowAdaptiveInfo())

                    NavigationSuiteScaffoldLayout(
                        navigationSuite = {
                            // Custom Navigation Rail with centered items.
                            if (navSuiteType == NavigationSuiteType.NavigationRail) {
                                NavigationRail {
                                   AppDestinations.entries.forEachIndexed { index, appDestinations ->
                                        NavigationRailItem(
                                            selected = selectedItem == index,
                                            onClick = { selectedItem = index },
                                            icon = { Icon(imageVector = appDestinations.icon,
                                                contentDescription = appDestinations.contentDescription.toString()) },
                                            label = { Text(text = appDestinations.label.toString()) }
                                        )
                                    }
                                }
                            } else {
                                NavigationSuite {
                                    AppDestinations.entries.forEachIndexed { index, appDestinations ->
                                        item(
                                            selected = selectedItem == index,
                                            onClick = { selectedItem = index },
                                            icon = { Icon(imageVector = appDestinations.icon,
                                                contentDescription = appDestinations.contentDescription.toString()) },
                                            label = { Text(text = appDestinations.label.toString()) }
                                        )
                                    }
                                }
                            }
                        }
                    ) {
                        // Screen content.
                        Text(
                            modifier = Modifier.padding(16.dp),
                            text = "Current NavigationSuiteType: $navSuiteType"
                        )
                    }
            }
        }
    }
}

enter image description here


Solution

  • Text(text = appDestinations.label.toString())
    

    You are calling toString() on an Int. That will result in a string representation of the number. The fact that you annotated it with @StringRes just means that Lint will help ensure that the number references a string resource.

    If you want to actually get the string associated with the resource id, use stringResource(Int).

    Text(text = stringResource(appDestinations.label))