Professional Documents
Culture Documents
Disusun Oleh :
1. Langkah 1
Buatlah project baru dengan nama “Aplikasi Login Sederhana.
2. Langkah 2
Kemudian Buat activity baru dengan cara Klik Kanan app > New > Activity > pilih Empty
Activity > kemudian pada Activity name.
3. Langkah 3
Setelah itu silahkan buka activity_main.xml, klik mode Text, lalu memasukkan listing kode
berikut ke dalam file activity_main.xml.
<EditText
android:id="@+id/editTextTextNIM"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:backgroundTint="@color/white"
android:ems="10"
android:hint="masukan nim"
android:inputType="textEmailAddress"
android:textColor="@color/white"
android:textColorHint="@color/colorLightAsh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:backgroundTint="@color/white"
android:ems="10"
android:hint="masukan password"
android:inputType="textPassword"
android:textColor="@color/white"
android:textColorHint="@color/colorLightAsh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="24dp"
android:text="@string/login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="166dp"
android:layout_marginEnd="16dp"
android:text="@string/sign_in"
android:textColor="@color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:text="@string/nim"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:text="@string/password"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextNIM" />
</androidx.constraintlayout.widget.ConstraintLayout>
5. Langkah 5
Setelah selesai mendesain tampilan aplikasi kita, sekarang kita buka MainActivity.kt dan
masukkan listing kode di bawah ini:
package com.ur.apps2go
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.ur.apps2go.api.RetrofitClient
import com.ur.apps2go.model.LoginResponse
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
buttonLogin.setOnClickListener {
val nim = editTextTextNIM.text.toString().trim()
val password = editTextTextPassword.text.toString().trim()
if(nim.isEmpty()){
editTextTextNIM.error = "Email required"
editTextTextNIM.requestFocus()
return@setOnClickListener
}
if(password.isEmpty()){
editTextTextPassword.error = "Password required"
editTextTextPassword.requestFocus()
return@setOnClickListener
}
RetrofitClient.instance.userLogin(nim, password)
.enqueue(object: Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t:Throwable) {
Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show()
}
startActivity(intent)
}else {
Toast.makeText(applicationContext, response.body()?.message, Toast.LENGTH_LONG).show()
}
}
})
}
}
}
6. Langkah 6
Kemudian buka AndroidManifest.xml dan masukkan listing kode di bawah ini:
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Apps2Go">
<activity android:name=".Dashboard"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
7. Langkah 7
Kemudian Buat packace baru yaitu model terus membuat class baru dengan cara Klik Kanan
model > New > class > pilih data class > kemudian kasih nama Loginresponse, masukkan
listing kode di bawah ini:
package com.ur.apps2go.model
8. Langkah 8
Kemudian Buat packace baru yaitu API terus membuat class baru dengan cara Klik Kanan
model > New > class > pilih Interfase > kemudian kasih nama API, masukkan listing kode di
bawah ini:
package com.ur.apps2go.api
import com.ur.apps2go.model.LoginResponse
import retrofit2.Call
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST
interface Api {
@FormUrlEncoded
@POST( "userlogin")
fun userLogin(
@Field( value = "email")email:String,
@Field( value =
"password")password:String
): Call<LoginResponse>
}
9. Langkah 9
Kemudian Buat class baru di dalam packace API dengan cara Klik Kanan model > New >
class > pilih Object > kemudian kasih nama Retrofit Client, masukkan listing kode di bawah
ini:
package com.ur.apps2go.api
import com.ur.apps2go.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object RetrofitClient {
private const val BASE_URL = "http://34.101.73.253:8081"
retrofit.create(Api::class.java)
}
}
10. Langkah 10
Kemudian yang terakhir buka build.gradle(:app) dan masukkan listing kode di bawah ini:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.ur.apps2go"
minSdkVersion 27
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles
getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility
JavaVersion.VERSION_1_8
targetCompatibility
JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-
stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation
'androidx.appcompat:appcompat:1.2.0'
implementation
'com.google.android.material:material:1.2.1'
implementation
'androidx.constraintlayout:constraintlayout:2.0.
4'
implementation
'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-
livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-
viewmodel-ktx:2.2.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation
'androidx.test.ext:junit:1.1.2'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.3.0'
implementation
'com.squareup.retrofit2:retrofit:2.5.0'
implementation
'com.squareup.retrofit2:converter-gson:2.5.0'
implementation
'com.squareup.okhttp3:logging-
interceptor:3.8.0'
Simpan project kita dan jalankan Aplikasi kita dengan cara klik tombol Run atau melalui
menu Run > Run ‘app’ kemudian memilih perangkat yang Anda gunakan. kita bisa
menggunakan Android Virtual Device (AVD) atau menggunakan ponsel Anda melalui USB
untuk Run app Anda.
DEMO APLIKASI
Setelah aplikasi di jalankan, hasilnya akan seperti gambar screenshot dibawah ini:
Selesai. Kita juga bisa membuat desain sesuai versi kita sendiri, atau bisa juga mengganti tulisan
keterangan sesuai dengan yang kita inginkan.