Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views8 pages

Tugas 2 Android

The document outlines the steps to create a Crypto Android application using Android Studio with Kotlin. It includes instructions for setting up the project, configuring dependencies, creating necessary classes and layouts, and implementing the main activity to fetch and display cryptocurrency data from an API. The project builds successfully, but it encounters a crash when run.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Tugas 2 Android

The document outlines the steps to create a Crypto Android application using Android Studio with Kotlin. It includes instructions for setting up the project, configuring dependencies, creating necessary classes and layouts, and implementing the main activity to fetch and display cryptocurrency data from an API. The project builds successfully, but it encounters a crash when run.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Nurul Syarifah

(044334393)
Tugas 2: Pemgrograman Berbasis Perangkat Bergerak

Membuat Aplikasi Crypto Android dengan Android Studio


• Klik New Project > Pilih Empty Activity > Next
• Isi Name: CryptoApp
• Pilih bahasa Kotlin
• Minimum SDK: pilih yang kamu mau, misal API 21 (Lollipop)
• Finish
Dengan penatalaksanaan berikut ini:

1. build.gradle.kts (Module: app)


Klik Gradle Scripts > build.gradle (Module: app)
Untuk: Menambahkan dependensi seperti Retrofit, RecyclerView, Gson (untuk fetch dan
menampilkan data dari API).

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.example.cryptoapp"
compileSdk = 35

defaultConfig {
applicationId = "com.example.cryptoapp"
minSdk = 22
targetSdk = 35
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = "11"
}

buildFeatures {
viewBinding = true
}
}

dependencies {
// AndroidX dasar
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")

// RecyclerView
implementation("androidx.recyclerview:recyclerview:1.3.1")

// Retrofit + Gson
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")

// Testing
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

Lalu klik Sync Now di pojok kanan atas Android Studio.

2.Permission Internet di AndroidManifest.xml


Buka app/src/main/AndroidManifest.xml tulis
Untuk menentukan izin internet untuk aplikasi mobile.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cryptoapp">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:label="CryptoApp"
android:allowBackup="true"
android:supportsRtl="true">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

3. ApiService.kt
Klik kanan pada package (com/yourname/projectname) → New → Kotlin Class/File → beri
nama: ApiService.kt
Fungsinya untuk Interface Retrofit untuk mengambil data dari API menggunakan anotasi
@GET.
package com.example.cryptoapp

import retrofit2.Call
import retrofit2.http.GET

interface ApiService {
@GET("tickers/")
fun getCryptos(): Call<CryptoResponse>
}

4. CryptoItem.kt — Model data


app/src/main/java/com/yourname/projectname/CryptoItem.kt
Untuk mempresentasi data satu item crypto (name, symbol, price_usd) sesuai struktur JSON
API.
package com.example.cryptoapp

data class CryptoItem(


val id: String,
val symbol: String,
val name: String,
val price_usd: String
)

5. CryptoResponse.kt
app/src/main/java/com/yourname/projectname/CryptoResponse.kt
Untuk menyusun struktur respons dari API tickers yang isinya list data.
package com.example.cryptoapp

data class CryptoResponse(


val data: List<Crypto>
)

data class Crypto(


val name: String,
val symbol: String,
val price_usd: String
)

6. CryptoAdapter.kt — Adapter RecyclerView


app/src/main/java/com/yourname/projectname/CryptoAdapter.kt
Fungsinya sebagai Adapter RecyclerView untuk menghubungkan data dengan layout
item_crypto.xml.
package com.example.cryptoapp

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class CryptoAdapter(private val cryptoList: List<Crypto>) :


RecyclerView.Adapter<CryptoAdapter.CryptoViewHolder>() {

class CryptoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {


val txtName: TextView = itemView.findViewById(R.id.txtName)
val txtSymbol: TextView = itemView.findViewById(R.id.txtSymbol)
val txtPrice: TextView = itemView.findViewById(R.id.txtPrice)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CryptoViewHolder {


val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_crypto, parent, false)
return CryptoViewHolder(view)
}

override fun onBindViewHolder(holder: CryptoViewHolder, position: Int) {


val crypto = cryptoList[position]
holder.txtName.text = crypto.name
holder.txtSymbol.text = crypto.symbol
holder.txtPrice.text = "USD ${crypto.price_usd}"
}

override fun getItemCount(): Int = cryptoList.size


}

7. activity_main.xml — Layout utama dengan RecyclerView dan Button


app/src/main/res/layout/activity_main.xml
Berfungsi sebagai layout utama, berisi tombol "Get Data" dan RecyclerView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<Button
android:id="@+id/btnFetch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Data" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
8. item_crypto.xml — Layout tiap item list
app/src/main/res/layout/item_crypto.xml
Berfungsi sebagai Layout 1 item tampilan data crypto dalam list (isi: name, symbol, price).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/txtName"
android:textStyle="bold"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/txtSymbol"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/txtPrice"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

9. MainActivity.kt — Activity utama yang menghubungkan semua


app/src/main/java/com/yourname/projectname/MainActivity.kt
Sebagai Logika utama — tombol ambil data, API call, dan menampilkan hasil ke RecyclerView.
package com.example.cryptoapp

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import retrofit2.*
import retrofit2.converter.gson.GsonConverterFactory

class MainActivity : AppCompatActivity() {


private lateinit var recyclerView: RecyclerView
private lateinit var adapter: CryptoAdapter

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val btnFetch: Button = findViewById(R.id.btnFetch)


recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)

btnFetch.setOnClickListener {
fetchCryptoData()
}
}

private fun fetchCryptoData() {


val retrofit = Retrofit.Builder()
.baseUrl("https://api.coinlore.net/api/")
.addConverterFactory(GsonConverterFactory.create())
.build()

val api = retrofit.create(ApiService::class.java)

api.getCryptos().enqueue(object : Callback<CryptoResponse> {
override fun onResponse(
call: Call<CryptoResponse>,
response: Response<CryptoResponse>
){
if (response.isSuccessful) {
val cryptos = response.body()?.data ?: emptyList()
adapter = CryptoAdapter(cryptos)
recyclerView.adapter = adapter
}
}

override fun onFailure(call: Call<CryptoResponse>, t: Throwable) {


t.printStackTrace()
}
})
}
}
Hasil: Proyek sudah selesai dan build berhasil, namun terjadi crash saat dijalankan

You might also like