Monday, October 11, 2021

jetpack compose - How to use bottom navigation

MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.ThumbUp
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            GetScaffold()
        }
    }


    @Composable
    fun GetScaffold(){
        val selectedItem = remember { mutableStateOf("Add")}

        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text(
                        text = "Compose - Bottom Navigation"
                    )},
                    backgroundColor = Color(0xFFC0E8D5),
                )
            },
            content = {MainContent(selectedItem)},
            backgroundColor = Color(0xFFEDEAE0),
            bottomBar = { BottomBarContent(selectedItem) },
            floatingActionButton = {
                FloatingActionButton(
                    onClick = {
                        // do something here
                    },
                    shape = CutCornerShape(45.dp)
                ) {
                    Icon(Icons.Filled.Favorite, contentDescription = "")
                }
            },
            isFloatingActionButtonDocked = true
        )
    }


    @Composable
    fun BottomBarContent(selectedItem: MutableState<String>){
        BottomAppBar(
            elevation = 4.dp,
            cutoutShape = CutCornerShape(45.dp)
        ) {
            BottomNavigation() {
                BottomNavigationItem(
                    icon = {Icon(Icons.Filled.AddCircle,"")},
                    selected = selectedItem.value == "Add",
                    onClick = { selectedItem.value = "Add" },
                    label = {Text(text = "Add")},
                    alwaysShowLabel = false
                )

                BottomNavigationItem(
                    icon = {Icon(Icons.Filled.ThumbUp,"")},
                    selected = selectedItem.value == "ThumbUp",
                    onClick = { selectedItem.value = "ThumbUp" },
                    label = {Text(text = "ThumbUp")},
                    alwaysShowLabel = false
                )

                BottomNavigationItem(
                    icon = {Icon(Icons.Filled.Delete,"")},
                    selected = selectedItem.value == "Delete",
                    onClick = { selectedItem.value = "Delete" },
                    label = {Text(text = "Delete")},
                    alwaysShowLabel = false
                )
            }
        }
    }


    @Composable
    fun MainContent(selectedItem: MutableState<String>){
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ){
            Text(
                text = "Selected ${selectedItem.value}",
                fontWeight = FontWeight.Bold
            )
        }
    }
}