Untitled
unknown
plain_text
a year ago
2.7 kB
9
Indexable
# Custom Android Home Screen Layout with Jetpack Compose
```kotlin
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.*
import androidx.compose.foundation.Image
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyGridState
import androidx.compose.foundation.lazy.grid.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painterResource
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
HomeScreen()
}
}
}
@Composable
fun HomeScreen() {
val packageManager = LocalContext.current.packageManager
val apps = remember { getInstalledApps(packageManager) }
var gridSize by remember { mutableStateOf(3) }
LazyVerticalGrid(
columns = GridCells.Fixed(gridSize),
modifier = Modifier.fillMaxSize()
) {
items(apps) { app ->
AppIcon(app) {
// Handle app launch with animation
}
}
}
}
@Composable
fun AppIcon(app: AppInfo, onClick: () -> Unit) {
val scale = remember { Animatable(1f) }
Box(
modifier = Modifier
.size(64.dp)
.pointerInput(Unit) {
detectHorizontalDragGestures { _, _ ->
// Handle swipe down gesture to close app
}
}
.clickable {
onClick()
// Trigger scaling animation
scale.animateTo(0.9f, animationSpec = tween(100))
scale.animateTo(1f, animationSpec = tween(100))
}
) {
Image(
painter = painterResource(id = app.iconResId),
contentDescription = app.name,
modifier = Modifier.scale(scale.value)
)
}
}
data class AppInfo(val name: String, val iconResId: Int)
fun getInstalledApps(packageManager: PackageManager): List<AppInfo> {
val apps = mutableListOf<AppInfo>()
val packages = packageManager.getInstalledApplications(0)
for (packageInfo in packages) {
apps.add(AppInfo(packageInfo.loadLabel(packageManager).toString(), packageInfo.icon))
}
return apps
}
Editor is loading...
Leave a Comment