Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active October 1, 2023 01:57
Show Gist options
  • Save benigumocom/72d56a21d1cc0a22beac55688733cd87 to your computer and use it in GitHub Desktop.
Save benigumocom/72d56a21d1cc0a22beac55688733cd87 to your computer and use it in GitHub Desktop.
Material3 Theme for coloring status bar and navigation bar
package com.example.myapplication.ui.theme
import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat
@Composable
fun MyApplicationTheme(
dynamicColor: Boolean = false,
content: @Composable () -> Unit
) {
val canDynamicColor = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S // Android 12 (API-31)
val darkTheme = isSystemInDarkTheme()
val colorScheme = if (dynamicColor && canDynamicColor) {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
} else {
if (darkTheme) DarkColorScheme else LightColorScheme
}
val view = LocalView.current
if (!view.isInEditMode) {
val window = (view.context as Activity).window
val color = colorScheme.primary.toArgb()
SideEffect {
window.apply {
statusBarColor = color
navigationBarColor = color
}
WindowCompat.getInsetsController(window, view).apply {
isAppearanceLightStatusBars = darkTheme
isAppearanceLightNavigationBars = darkTheme
}
}
}
MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
}
package com.example.myapplication.ui.theme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.ui.graphics.Color
val DarkColorScheme = darkColorScheme(
primary = Color(0xFFD0BCFF), // Purple80
secondary = Color(0xFFCCC2DC), // PurpleGrey80
tertiary = Color(0xFFEFB8C8) // Pink80
)
val LightColorScheme = lightColorScheme(
primary = Color(0xFF6650a4), // Purple40
secondary = Color(0xFF625b71), // PurpleGrey40
tertiary = Color(0xFF7D5260), // Pink40
// background = Color(0xFFFFFBFE),
// surface = Color(0xFFFFFBFE),
// onPrimary = Color.White,
// onSecondary = Color.White,
// onTertiary = Color.White,
// onBackground = Color(0xFF1C1B1F),
// onSurface = Color(0xFF1C1B1F),
)
package com.example.myapplication.ui.theme
import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
val Typography = Typography(
bodyLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp
),
// titleLarge = TextStyle(
// fontFamily = FontFamily.Default,
// fontWeight = FontWeight.Normal,
// fontSize = 22.sp,
// lineHeight = 28.sp,
// letterSpacing = 0.sp
// ),
// labelSmall = TextStyle(
// fontFamily = FontFamily.Default,
// fontWeight = FontWeight.Medium,
// fontSize = 11.sp,
// lineHeight = 16.sp,
// letterSpacing = 0.5.sp
// )
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment