Tugas Mobile Programming - Praktikum 9 | Teknik Informatika UNPI

 NAMA                 : MUHAMMAD MUSLIM ABDUL JABBAAR

NIM                      : 217200035

PRODI                  : TEKNIK INFORMATIKA

MATKUL             : MOBILE PROGRAMMING 1



Shared Preferences







  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvName"
        android:text="Name"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvAge"
        android:text="Age"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvGender"
        android:text="Gender"
        android:textSize="24sp"/>

    <Button
        android:id="@+id/btnEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EDIT"
        android:layout_gravity="center"/>

</LinearLayout>

  • activity_form.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvName"
        android:text="Name"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvAge"
        android:text="Age"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvGender"
        android:text="Gender"
        android:textSize="24sp"/>

    <Button
        android:id="@+id/btnEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EDIT"
        android:layout_gravity="center"/>

</LinearLayout>

  • MainActivity.kt
package com.opendebugger62.pertemuan11

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    lateinit var profilePref : ProfilePref
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val tvName = findViewById<TextView>(R.id.tvName)
        val tvAge = findViewById<TextView>(R.id.tvAge)
        val tvGender = findViewById<TextView>(R.id.tvGender)
        profilePref = ProfilePref(this)
        if (profilePref.preference.contains(ProfilePref.Name)) {
            val profile : Profile = profilePref.getProfile()
            tvName.text = profile.name
            tvAge.text = profile.age.toString()
            tvGender.text = profile.gender
        }
        val btnEdit = findViewById<View>(R.id.btnEdit)
        btnEdit.setOnClickListener {
            Intent(this, FormActivity::class.java).also {
                startActivity(it)
            }
        }
    }
}

  • FormActivity.kt
package com.opendebugger62.pertemuan11

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast

class FormActivity : AppCompatActivity() {
    lateinit var profilePref: ProfilePref
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_form)
        val btnSave = findViewById<View>(R.id.btnSave)
        val etName = findViewById<EditText>(R.id.etName)
        val etAge = findViewById<EditText>(R.id.etAge)
        val etGender = findViewById<EditText>(R.id.etGender)
        profilePref = ProfilePref(this)
        if (profilePref.preference.contains(ProfilePref.Name)) {
            val profile : Profile = profilePref.getProfile()
            etName.setText(profile.name)
            profile.age?.let { etAge.setText(it.toString()) }
            etGender.setText(profile.gender)
        }
        btnSave.setOnClickListener {
            val name : String = etName.text.toString().trim()
            val age : String = etAge.text.toString().trim()
            val gender : String = etGender.text.toString().trim()
            val profile = Profile(name, age.toInt(), gender)
            saveToPref(profile)
        }
    }

    private fun saveToPref(profile: Profile) {
        profilePref.setProfile(profile)
        Toast.makeText(this, "Data berhasil disimpan", Toast.LENGTH_SHORT).show()
        Intent(this, MainActivity::class.java).also {
            it.flags=Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            startActivity(it)
        }

    }
}
  • Profile.kt
package com.opendebugger62.pertemuan11

data class Profile(
    var name : String? = null,
    var age : Int? = null,
    var gender : String? = null
)

  • PofilePref.kt
package com.opendebugger62.pertemuan11

import android.content.Context
import android.content.SharedPreferences
import android.icu.lang.UProperty.AGE

class ProfilePref(context : Context) {
    companion object {
        const val SP_Name = "profile_pref"
        const val Name = "name"
        const val Gender = "Gender"
    }

    val preference : SharedPreferences = context.getSharedPreferences(SP_Name, Context.MODE_PRIVATE)
    fun setProfile(profile : Profile) {
        val prefEditor : SharedPreferences.Editor = preference.edit()
        prefEditor.putString(Name, profile.name)
        profile.age?.let { prefEditor.putInt(AGE.toString(), it) }
        prefEditor.putString(Gender, profile.gender)
        prefEditor.apply()
    }
    fun getProfile(): Profile {
        val profile = Profile()
        profile.name = preference.getString(Name, "")
        profile.age = preference.getInt(AGE.toString(), 0)
        profile.gender = preference.getString(Gender, "")
        return profile
    }
}

Komentar

Postingan populer dari blog ini

Tugas Mobile Programming - Praktikum 5 | Teknik Informatika UNPI

Tugas Mobile Programming - Praktikum 8 | Teknik Informatika UNPI