Tugas 6 - PPB F
Nama: Nabila Zakiyah Khansa Machrus
NRP: 5025201139
NRP: 5025201139
Kelas: PPB F
Membuat Image Scroll dengan menggunakan Desain Material
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
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
AffirmationsTheme { | |
// A surface container using the 'background' color from the theme | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = MaterialTheme.colorScheme.background | |
) { | |
AffirmationsApp() | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun AffirmationsApp() { | |
AffirmationList( | |
affirmationList = Datasource().loadAffirmations(), | |
) | |
} | |
@Composable | |
fun AffirmationList(affirmationList: List<Affirmation>, modifier: Modifier = Modifier) { | |
LazyColumn(modifier = modifier) { | |
items(affirmationList) { affirmation -> | |
AffirmationCard( | |
affirmation = affirmation, | |
modifier = Modifier.padding(8.dp) | |
) | |
} | |
} | |
} | |
@Composable | |
fun AffirmationCard(affirmation: Affirmation, modifier: Modifier = Modifier) { | |
Card(modifier = modifier) { | |
Column { | |
Image( | |
painter = painterResource(affirmation.imageResourceId), | |
contentDescription = stringResource(affirmation.stringResourceId), | |
modifier = Modifier | |
.fillMaxWidth() | |
.height(194.dp), | |
contentScale = ContentScale.Crop | |
) | |
Text( | |
text = LocalContext.current.getString(affirmation.stringResourceId), | |
modifier = Modifier.padding(16.dp), | |
style = MaterialTheme.typography.headlineSmall | |
) | |
} | |
} | |
} | |
@Preview | |
@Composable | |
private fun AffirmationCardPreview() { | |
AffirmationCard(Affirmation(R.string.affirmation1, R.drawable.image1)) | |
} |
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.affirmations.model | |
import androidx.annotation.DrawableRes | |
import androidx.annotation.StringRes | |
data class Affirmation( | |
@StringRes val stringResourceId: Int, | |
@DrawableRes val imageResourceId: Int | |
) |
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
class Datasource() { | |
fun loadAffirmations(): List<Affirmation> { | |
return listOf<Affirmation>( | |
Affirmation(R.string.affirmation1, R.drawable.image1), | |
Affirmation(R.string.affirmation2, R.drawable.image2), | |
Affirmation(R.string.affirmation3, R.drawable.image3), | |
Affirmation(R.string.affirmation4, R.drawable.image4), | |
Affirmation(R.string.affirmation5, R.drawable.image5), | |
Affirmation(R.string.affirmation6, R.drawable.image6), | |
Affirmation(R.string.affirmation7, R.drawable.image7), | |
Affirmation(R.string.affirmation8, R.drawable.image8), | |
Affirmation(R.string.affirmation9, R.drawable.image9), | |
Affirmation(R.string.affirmation10, R.drawable.image10)) | |
} | |
} |
Komentar
Posting Komentar