Create a setting drawer which have 5 list tiles with icons in jetpack compose
- User
Create a setting drawer which have 5 list tiles with icons in jetpack compose
- Assistant
Create a setting drawer which have 5 list tiles with icons in jetpack compose
Creating a settings drawer in Jetpack Compose with 5 list tiles, each containing an icon, can be efficiently achieved using the Scaffold composable. The Scaffold provides a layout structure that implements the basic material design layout structure, including the top app bar, bottom navigation, floating action button, and importantly for this case, a navigation drawer.
Below is a step-by-step guide along with the complete code to create a settings drawer with 5 list tiles, each showcasing an icon and text.
Setup Your Project:
Define the Main Structure Using Scaffold:
Scaffold to structure your layout, specifying drawerContent for the navigation drawer.rememberScaffoldState() and control it with a coroutine scope.Create the Drawer Content:
drawerContent, use a Column to list your settings items.ListItem or Row with Icon and Text to create each list tile.Add Interaction:
Here's a complete example demonstrating how to create a settings drawer with 5 list tiles using Jetpack Compose:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Shield
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Info
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
class SettingsDrawerActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SettingsDrawerApp()
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsDrawerApp() {
val scaffoldState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
Scaffold(
topBar = {
TopAppBar(
title = { Text("Settings Drawer Example") },
navigationIcon = {
IconButton(onClick = {
scope.launch { scaffoldState.open() }
}) {
Icon(Icons.Default.Settings, contentDescription = "Menu")
}
}
)
},
content = { paddingValues ->
// Your main content goes here
Box(modifier = Modifier
.fillMaxSize()
.padding(paddingValues),
contentAlignment = androidx.compose.ui.Alignment.Center
) {
Text(text = "Main Content", style = MaterialTheme.typography.headlineMedium)
}
},
drawerContent = {
SettingsDrawerContent(
onItemClick = {
// Handle navigation or actions here
},
closeDrawerAction = {
scope.launch { scaffoldState.close() }
}
)
},
drawerState = scaffoldState
)
}
@Composable
fun SettingsDrawerContent(
onItemClick: (SettingsItem) -> Unit,
closeDrawerAction: () -> Unit
) {
val items = listOf(
SettingsItem.IconItem(
title = "Account",
icon = Icons.Default.AccountCircle
),
SettingsItem.IconItem(
title = "Notifications",
icon = Icons.Default.Notifications
),
SettingsItem.IconItem(
title = "Privacy",
icon = Icons.Default.Shield
),
SettingsItem.IconItem(
title = "Settings",
icon = Icons.Default.Settings
),
SettingsItem.IconItem(
title = "About",
icon = Icons.Default.Info
)
)
Column(modifier = Modifier.fillMaxSize()) {
Spacer(modifier = Modifier.height(24.dp))
// Optional: Add a header
Text(
text = "Settings",
style = MaterialTheme.typography.headlineSmall,
modifier = Modifier.padding(start: 16.dp, bottom: 16.dp)
)
// List of settings items
items.forEach { item ->
Row(
modifier = Modifier
.fillMaxWidth()
.clickable {
onItemClick(item)
closeDrawerAction()
}
.padding(vertical = 12.dp, horizontal = 16.dp),
verticalAlignment = androidx.compose.ui.Alignment.CenterVertically
) {
Icon(
imageVector = item.icon,
contentDescription = item.title,
modifier = Modifier.size(24.dp)
)
Spacer(modifier = Modifier.width(16.dp))
Text(text = item.title, style = MaterialTheme.typography.bodyLarge)
}
}
}
}
sealed class SettingsItem {
data class IconItem(val title: String, val icon: androidx.compose.ui.graphics.vector.ImageVector) : SettingsItem()
}
Main Activity (SettingsDrawerActivity):
SettingsDrawerApp() which contains the entire UI.Scaffold (SettingsDrawerApp Composable):
scaffoldState: Manages the state of the drawer (opened or closed).TopAppBar: Contains a navigation icon (Settings icon) that, when clicked, opens the drawer.content: Represents the main screen content. Here, it's a simple centered text but you can replace it with your actual content.drawerContent: Calls SettingsDrawerContent to define the content of the drawer.Drawer Content (SettingsDrawerContent Composable):
items: A list of SettingsItem.IconItem each representing a setting option with a title and an icon.onItemClick: A lambda to handle item click events. You can navigate to different screens or perform other actions here.closeDrawerAction: Closes the drawer when an item is clicked.SettingsItem Sealed Class:
IconItem which has a title and an icon. This makes it easier to extend in the future if you want to add different types of items.Icons:
AccountCircle, Notifications, Shield, Settings, and Info to represent different settings.Styling and Spacing:
Icons and Titles:
items list.Handling Clicks:
onItemClick lambda to make the drawer functional.Styling:
Make sure you have the necessary dependencies in your build.gradle file for Jetpack Compose and Material3:
dependencies {
implementation "androidx.compose.material3:material3:1.1.0" // Use the latest version
implementation "androidx.activity:activity-compose:1.7.0" // Use the latest version
// Other dependencies
}
This example provides a foundational structure for a settings drawer in an Android application using Jetpack Compose. You can expand upon this by adding navigation to different screens, integrating state management, and enhancing the UI according to your app's design language.