This document discusses using Jetpack Compose for building Android user interfaces. It provides an overview of key Compose concepts like layouts, basic elements like Text and Image, theming with Material Design, and navigation between screens. The document includes code examples for defining a data model, applying a theme, centering text, displaying weather state, and navigating between a main screen and forecast screen using a Compose model object. It demonstrates hands-on usage of Compose through building a sample weather app.
14. @Composable
private fun MainScreen(){
Column {
DrawBackground()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
MainScreen()
}
}
}
@Composable
fun DrawBackground() {
DrawImage(image =
imageResource(R.drawable.nyc_night_1))
}
B a c k g r o u n d
16. BONSAI|PRESENTATION
data class CurrentWeather(
val locationName: String,
val curTemperature: Temperature,
val forecast: WeatherForecast
)
class Temperature(val celcius: Double)
data class WeatherForecast(
val maxTemperature: Temperature,
val minTemperature: Temperature,
val state: WeatherState
)
enum class WeatherState(@DrawableRes val iconResId: Int,
@StringRes val descriptionResId: Int) {
SUNNY(R.drawable.ic_sunny, R.string.state_sunny),
RAINY(R.drawable.ic_rainy, R.string.state_rainy),
PARTLY_CLOUDY(R.drawable.ic_p_cloudy, R.string.state_p_cloudy),
SNOWY(R.drawable.ic_snowy, R.string.state_snowy)
}
<string name="state_sunny">Sunny</string>
<string name="state_rainy">Rainy</string>
D a t a M o d e l s
17. @Composable
fun Title(currentWeather: CurrentWeather) {
Text(text = currentWeather.locationName,
style = MaterialTheme.typography().h2
.copy(color = Color.White,
fontWeight = FontWeight.W200,
fontFamily = FontFamily("sans-
serif-thin")))
}
}
T i t l e
19. A p p l y T h e m e
setContent {
WeatherTheme {
Column {
DrawBackground()
Title(currentWeather)
}
}
}
@Composable
private fun Title(cur: CurrentWeather) {
Text(
text = cur.locationName,
style = MaterialTheme.typography().h2
)
}
20. @Composable
fun Title(cur: CurrentWeather) {
Row(modifier = LayoutWidth.Fill,
arrangement = Arrangement.Center) {
Spacer(LayoutHeight(16.dp))
Text(text = currentWeather.locationName,
style = MaterialTheme.typography().h2)
}
}
C e n t e r
Column {
DrawBackground()
Title(currentWeather)
}
21. T o d a y s
S t a t eRow(
modifier = LayoutWidth.Fill,
arrangement = Arrangement.Center) {
Container(width = 32.dp, height = 32.dp) {
DrawImage(image = imageResource(
currentWeather.forecast.state.iconResId))
}
// Empty Space.
Spacer(LayoutWidth(8.dp)
Text(
text = stringResource(
currentWeather.forecast.state.descriptionResId),
style = MaterialTheme.typography().body2)
}
22. T o d a y s
F o r e c a s t
@Composable
fun CurrentWeatherBlock(
current: CurrentWeather) {
Column(modifier =
LayoutWidth.Fill ,
arrangement =
Arrangement.Center) {
}
}
WeatherState(current)
CurrentTemperature(current)
MinMaxTemperatures(current)
23. L a y o u t
A d j u s t
Column {
DrawBackground()
Title(currentWeather = cur)
Spacer(modifier = LayoutFlexible(0.7f))
CurrentWeatherBlock(currentWeather = cur)
Spacer(modifier = LayoutFlexible(0.3f))
}
24. L i s t E l e m e n t
fun WeatherForecastRow
Row {
}
Card(color = cardBackgroundColor, elevation = 8.dp) {
Padding(left = 8.dp, right = 8.dp, top = 16.dp, bottom = 16.dp) {
WeatherForecastRow(forecast)
}
}
for (forecast in forecasts) {
}