package com.example.jetpackcompose.material import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.compose.material.Icon import androidx.compose.material.Text import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.BottomNavigation import androidx.compose.material.BottomNavigationItem import androidx.compose.material.Card import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Favorite import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.activity.compose.setContent import androidx.compose.ui.unit.dp import androidx.compose.ui.tooling.preview.Preview import com.example.jetpackcompose.image.TitleComponent class BottomNavigationActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // This is an extension function of Activity that sets the @Composable function that's // passed to it as the root view of the activity. This is meant to replace the .xml file // that we would typically set using the setContent(R.id.xml_file) method. The setContent // block defines the activity's layout. setContent { // Column is a composable that places its children in a vertical sequence. You // can think of it similar to a LinearLayout with the vertical orientation. Column() { // Title Component is a custom composable that we created which is capable of // rendering text on the screen in a certain font style & text size. TitleComponent("This is a simple bottom navigation bar that always shows label") // Card composable is a predefined composable that is meant to represent // the card surface as specified by the Material Design specification. We // also configure it to have rounded corners and apply a modifier. // You can think of Modifiers as implementations of the decorators pattern that // are used to modify the composable that its applied to. In this example, we assign // a padding of 8dp to the Card. Card(shape = RoundedCornerShape(4.dp), modifier = Modifier.padding(8.dp)) { BottomNavigationAlwaysShowLabelComponent() } TitleComponent( "This is a bottom navigation bar that only shows label for " + "selected item" ) Card(shape = RoundedCornerShape(4.dp), modifier = Modifier.padding(8.dp)) { BottomNavigationOnlySelectedLabelComponent() } } } } } val listItems = listOf("Games", "Apps", "Movies", "Books") // We represent a Composable function by annotating it with the @Composable annotation. Composable // functions can only be called from within the scope of other composable functions. We should // think of composable functions to be similar to lego blocks - each composable function is in turn // built up of smaller composable functions. @Composable fun BottomNavigationAlwaysShowLabelComponent() { // Reacting to state changes is the core behavior of Compose. You will notice a couple new // keywords that are compose related - remember & mutableStateOf.remember{} is a helper // composable that calculates the value passed to it only during the first composition. It then // returns the same value for every subsequent composition. Next, you can think of // mutableStateOf as an observable value where updates to this variable will redraw all // the composable functions that access it. We don't need to explicitly subscribe at all. Any // composable that reads its value will be recomposed any time the value // changes. This ensures that only the composables that depend on this will be redraw while the // rest remain unchanged. This ensures efficiency and is a performance optimization. It // is inspired from existing frameworks like React. var selectedIndex by remember { mutableStateOf(0) } // BottomNavigation is a component placed at the bottom of the screen that represents primary // destinations in your application. BottomNavigation(modifier = Modifier.padding(16.dp)) { listItems.forEachIndexed { index, label -> // A composable typically used as part of BottomNavigation. Since BottomNavigation // is usually used to represent primary destinations in your application, // BottomNavigationItem represents a singular primary destination in your application. BottomNavigationItem( icon = { // Simple composable that allows you to draw an icon on the screen. It // accepts a vector asset as the icon. Icon(imageVector = Icons.Filled.Favorite, contentDescription = "Favorite") }, label = { // Text is a predefined composable that does exactly what you'd expect it to - // display text on the screen. It allows you to customize its appearance using the // style property. Text(text = label) }, // Update the selected index when the BottomNavigationItem is clicked selected = selectedIndex == index, onClick = { selectedIndex = index } ) } } } @Composable fun BottomNavigationOnlySelectedLabelComponent() { // Reacting to state changes is the core behavior of Compose. You will notice a couple new // keywords that are compose related - remember & mutableStateOf.remember{} is a helper // composable that calculates the value passed to it only during the first composition. It then // returns the same value for every subsequent composition. Next, you can think of // mutableStateOf as an observable value where updates to this variable will redraw all // the composable functions that access it. We don't need to explicitly subscribe at all. Any // composable that reads its value will be recomposed any time the value // changes. This ensures that only the composables that depend on this will be redraw while the // rest remain unchanged. This ensures efficiency and is a performance optimization. It // is inspired from existing frameworks like React. var selectedIndex by remember { mutableStateOf(0) } // BottomNavigation is a component placed at the bottom of the screen that represents primary // destinations in your application. BottomNavigation(modifier = Modifier.padding(16.dp)) { listItems.forEachIndexed { index, label -> // A composable typically used as part of BottomNavigation. Since BottomNavigation // is usually used to represent primary destinations in your application, // BottomNavigationItem represents a singular primary destination in your application. BottomNavigationItem( icon = { // Simple composable that allows you to draw an icon on the screen. It // accepts a vector asset as the icon. Icon(imageVector = Icons.Filled.Favorite, contentDescription = "Favorite") }, label = { // Text is a predefined composable that does exactly what you'd expect it to - // display text on the screen. It allows you to customize its appearance using the // style property. Text(text = label) }, selected = selectedIndex == index, // Update the selected index when the BottomNavigationItem is clicked onClick = { selectedIndex = index }, // Setting this to false causes the label to be show only for the navigation item // that is currently selected, like in the BottomNavigationAlwaysShowLabelComponent // component. alwaysShowLabel = false ) } } } // Android Studio lets you preview your composable functions within the IDE itself, instead of // needing to download the app to an Android device or emulator. This is a fantastic feature as you // can preview all your custom components(read composable functions) from the comforts of the IDE. // The main restriction is, the composable function must not take any parameters. If your composable // function requires a parameter, you can simply wrap your component inside another composable // function that doesn't take any parameters and call your composable function with the appropriate // params. Also, don't forget to annotate it with @Preview & @Composable annotations. @Preview @Composable fun BottomNavigationAlwaysShowLabelComponentPreview() { BottomNavigationAlwaysShowLabelComponent() } @Preview @Composable fun BottomNavigationOnlySelectedLabelComponentPreview() { BottomNavigationOnlySelectedLabelComponent() }