Tugas 9 - Material Design - Aplikasi Woof
Code Untuk Tugas 8 Image Scroll
Nama : Fitra Agung Diassyah Putra
NRP : 5025201072
Kelas : PBB I
Hasil Image Scroll
Code Untuk MainActivity.kt
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
package com.example.woof | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.annotation.DrawableRes | |
import androidx.annotation.StringRes | |
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.foundation.lazy.items | |
import androidx.compose.material3.Card | |
import androidx.compose.material3.CenterAlignedTopAppBar | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Scaffold | |
import androidx.compose.material3.Surface | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.clip | |
import androidx.compose.ui.layout.ContentScale | |
import androidx.compose.ui.res.dimensionResource | |
import androidx.compose.ui.res.painterResource | |
import androidx.compose.ui.res.stringResource | |
import androidx.compose.ui.tooling.preview.Preview | |
import com.example.woof.data.Dog | |
import com.example.woof.data.dogs | |
import com.example.woof.ui.theme.WoofTheme | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
WoofTheme { | |
Surface( | |
modifier = Modifier.fillMaxSize() | |
) { | |
WoofApp() | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun WoofApp() { | |
Scaffold( | |
topBar = { | |
WoofTopAppBar() | |
} | |
) { it -> | |
LazyColumn(contentPadding = it) { | |
items(dogs) { | |
DogItem( | |
dog = it, | |
modifier = Modifier.padding(dimensionResource(R.dimen.padding_small)) | |
) | |
} | |
} | |
} | |
} | |
@Composable | |
fun DogItem( | |
dog: Dog, | |
modifier: Modifier = Modifier | |
) { | |
Card( | |
modifier = modifier | |
) { | |
Row( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(dimensionResource(R.dimen.padding_small)) | |
) { | |
DogIcon(dog.imageResourceId) | |
DogInformation(dog.name, dog.age) | |
} | |
} | |
} | |
@Composable | |
fun WoofTopAppBar(modifier: Modifier = Modifier) { | |
CenterAlignedTopAppBar( | |
title = { | |
Row( | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
Image( | |
modifier = Modifier | |
.size(dimensionResource(R.dimen.image_size)) | |
.padding(dimensionResource(R.dimen.padding_small)), | |
painter = painterResource(R.drawable.ic_woof_logo), | |
contentDescription = null | |
) | |
Text( | |
text = stringResource(R.string.app_name), | |
style = MaterialTheme.typography.displayLarge | |
) | |
} | |
}, | |
modifier = modifier | |
) | |
} | |
@Composable | |
fun DogIcon( | |
@DrawableRes dogIcon: Int, | |
modifier: Modifier = Modifier | |
) { | |
Image( | |
modifier = modifier | |
.size(dimensionResource(R.dimen.image_size)) | |
.padding(dimensionResource(R.dimen.padding_small)) | |
.clip(MaterialTheme.shapes.small), | |
contentScale = ContentScale.Crop, | |
painter = painterResource(dogIcon), | |
contentDescription = null | |
) | |
} | |
@Composable | |
fun DogInformation( | |
@StringRes dogName: Int, | |
dogAge: Int, | |
modifier: Modifier = Modifier | |
) { | |
Column(modifier = modifier) { | |
Text( | |
text = stringResource(dogName), | |
style = MaterialTheme.typography.displayMedium, | |
modifier = Modifier.padding(top = dimensionResource(R.dimen.padding_small)) | |
) | |
Text( | |
text = stringResource(R.string.years_old, dogAge), | |
style = MaterialTheme.typography.bodyLarge | |
) | |
} | |
} | |
@Preview | |
@Composable | |
fun WoofPreview() { | |
WoofTheme(darkTheme = false) { | |
WoofApp() | |
} | |
} | |
@Preview | |
@Composable | |
fun WoofDarkThemePreview() { | |
WoofTheme(darkTheme = true) { | |
WoofApp() | |
} | |
} |
Komentar
Posting Komentar