Instantly share code, notes, and snippets.
Last active
May 3, 2024 17:24
-
Star
0
(0)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save amoozeshbebin/6267cdb22df634e419712d389707f8ed to your computer and use it in GitHub Desktop.
Navigation Fragment in jetpack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Implementation: | |
implementation("androidx.navigation:navigation-compose:2.7.2") | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
MainActivity.kt: | |
import android.annotation.SuppressLint | |
import androidx.compose.material3.ExperimentalMaterial3Api | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.shape.CornerSize | |
import androidx.compose.material.icons.Icons | |
import androidx.compose.material.icons.filled.Add | |
import androidx.compose.material3.BottomAppBar | |
import androidx.compose.material3.FloatingActionButton | |
import androidx.compose.material3.Icon | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Scaffold | |
import androidx.compose.material3.Surface | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.TextField | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.saveable.rememberSaveable | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.navigation.compose.NavHost | |
import androidx.navigation.compose.composable | |
import androidx.navigation.compose.rememberNavController | |
import com.example.navigation.component.listView | |
import com.example.navigation.component.textfield | |
import com.example.navigation.component.utils.BottomNavigationView | |
import com.example.navigation.ui.theme.NavigationTheme | |
class MainActivity : ComponentActivity() { | |
private var dataList = ArrayList<String>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
NavigationTheme { | |
// A surface container using the 'background' color from the theme | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = MaterialTheme.colorScheme.background | |
) { | |
materialView() | |
} | |
} | |
} | |
} | |
@OptIn(ExperimentalMaterial3Api::class) | |
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") | |
@Preview(showBackground = true) | |
@Composable | |
fun materialView() { | |
val navController = rememberNavController() | |
Scaffold( | |
bottomBar = | |
{ | |
BottomNavigationView(navController) | |
}, | |
floatingActionButton = { | |
FloatingActionButton(onClick = { /*TODO*/ }) { | |
Icon(Icons.Filled.Add, contentDescription = "") | |
} | |
} | |
) { | |
NavHost( | |
navController = navController, | |
startDestination = "home", | |
) { | |
composable("home") { | |
textfield() | |
} | |
composable("list") { | |
listView(dataList) | |
} | |
} | |
} | |
} | |
private fun init() { | |
dataList.add("BMW E46") | |
dataList.add("Honda") | |
dataList.add("Benz") | |
dataList.add("Lambo") | |
dataList.add("BMW X6") | |
dataList.add("BMW X4") | |
} | |
} | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
NavigationItem.kt: | |
import com.example.navigation.R | |
open class NavigationItem(var route:String, var icon:Int, var title:String) { | |
object home :NavigationItem("home", R.drawable.baseline_home_24,"Home") | |
object list:NavigationItem("list",R.drawable.baseline_format_list_bulleted_24,"List") | |
} | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
BottomNavigationView.kt: | |
import androidx.compose.foundation.shape.CornerSize | |
import androidx.compose.material.BottomNavigationItem | |
import androidx.compose.material3.BottomAppBar | |
import androidx.compose.material3.Icon | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.res.painterResource | |
import androidx.navigation.NavController | |
import com.example.navigation.utils.NavigationItem | |
@Composable | |
fun BottomNavigationView(navController: NavController) { | |
val items = listOf( | |
NavigationItem.home, | |
NavigationItem.list | |
) | |
BottomAppBar { | |
MaterialTheme.shapes.small.copy( | |
CornerSize(percent = 50) | |
) | |
items.forEach { | |
BottomNavigationItem( | |
icon = { | |
Icon(painter = painterResource(id = it.icon), contentDescription = it.title) | |
}, | |
label = { Text(text = it.title) }, | |
selected = true, | |
onClick = { | |
navController.navigate(it.route) | |
} | |
) | |
} | |
} | |
} | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
ListView.kt: | |
import androidx.compose.foundation.layout.Spacer | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
@Composable | |
fun listView(dataList:ArrayList<String>) { | |
LazyColumn{ | |
items(dataList.size){index -> | |
Text(text = dataList[index]) | |
Spacer(modifier = Modifier.size(3.dp)) | |
} | |
} | |
} | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
TextField.kt: | |
import androidx.compose.material3.ExperimentalMaterial3Api | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.TextField | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.saveable.rememberSaveable | |
import androidx.compose.runtime.setValue | |
@OptIn(ExperimentalMaterial3Api::class) | |
@Composable | |
fun textfield(){ | |
var text by rememberSaveable { mutableStateOf("") } | |
TextField( | |
value = text, | |
onValueChange = { text = it }, | |
label = { Text("Label") }, | |
singleLine = true | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment