PPB I - Tugas Pert 8

       Tugas Pert 8

Nama : Rachel Anggieuli AP
NRP : 5025201263
Tahun : 2023/2024
Kelas : PPB I

Material Design

Pada tugas kali ini, ditugaskan untuk membuat "Image Scroll" pada android studio. Ditugaskan untuk membuat aplikasi yang menampilkan daftar afirmasi. Langkah pertama dalam mengkonfigurasi antarmuka pengguna untuk menampilkan daftar adalah membuat List Item. Setiap item terdiri dari gambar dan teks. Data untuk setiap item telah disiapkan sebelumnya, dan tugas kami adalah membuat komponen UI untuk menampilkan item tersebut. Setiap item menggunakan composable Card, yang berisi Image dan composable Text.

Link Tutorial
Link Tutorial-Github

Dokumentasi Aplikasi

Langkah Pengerjaan

Unduh file Zip Starter dari tautan yang diberikan dan buka file tersebut di Android Studio.
Tunggu hingga proses build gradle selesai dan jalankan aplikasinya. Selanjutnya, buat package bernama "model" di dalam "com.example.affirmations", lalu buat file bernama "Affirmation.kt" sebagai data class.
Class "Affirmation.kt" ini akan memiliki dua properti: "stringResourceId" yang menyimpan ID dari string affirmation dan "imageResourceId" yang menyimpan ID dari gambar affirmation. Kemudian, di dalam paket "com.example.affirmations.data", buka file "Datasource.kt", hapus tanda komentar pada dua pernyataan impor dan isi class Datasource.
Lengkapi kode di main sesuai dengan tutorial yang diberikan dan jalankan aplikasinya.
Source Code Aplikasi
MainActivity.kt
<>
package com.example.affirmations

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.affirmations.data.Datasource
import com.example.affirmations.ui.theme.AffirmationsTheme
import com.example.affirmations.model.Affirmation

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 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
            )
        }
    }
}

@Composable
fun AffirmationList(affirmationList: List<Affirmation<, modifier: Modifier = Modifier) {
    LazyColumn(modifier = modifier) {
        items(affirmationList) { affirmation ->
            AffirmationCard(
                affirmation = affirmation,
                modifier = Modifier.padding(8.dp)
            )
        }
    }
}

@Preview
@Composable
private fun AffirmationCardPreview() {
    AffirmationCard(Affirmation(R.string.affirmation1, R.drawable.image1))
}
<>
Datasource.kt
<>
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.affirmations.data

import com.example.affirmations.R
import com.example.affirmations.model.Affirmation

/**
 * [Datasource] generates a list of [Affirmation]
 */
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))
    }
}
<>
Affirmation.kt
<>
package com.example.affirmations.model

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes

data class Affirmation(
    @StringRes val stringResourceId: Int,
    @DrawableRes val imageResourceId: Int
)
<>

Comments

Popular posts from this blog

PPB I - Tugas Pert 11

PPB I - ETS