0
0
Android Kotlinmobile~20 mins

SharedPreferences / DataStore in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Settings Screen
This screen lets the user save and load a username using SharedPreferences. The user can enter a name, save it, and see the saved name displayed.
Target UI
-------------------------
|       Settings        |
|-----------------------|
| Enter username:       |
| [______________]      |
|                       |
| [ Save ]              |
|                       |
| Saved username:       |
| [No username saved]   |
-------------------------
Add an EditText for username input
Add a Save button that saves the username to SharedPreferences
Display the saved username below the button
Load and show the saved username when the screen opens
Starter Code
Android Kotlin
package com.example.settingsapp

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SettingsActivity : AppCompatActivity() {
    private lateinit var usernameEditText: EditText
    private lateinit var saveButton: Button
    private lateinit var savedUsernameTextView: TextView
    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        usernameEditText = findViewById(R.id.usernameEditText)
        saveButton = findViewById(R.id.saveButton)
        savedUsernameTextView = findViewById(R.id.savedUsernameTextView)

        sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)

        // TODO: Load saved username and display it

        // TODO: Set saveButton click listener to save username
    }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
package com.example.settingsapp

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SettingsActivity : AppCompatActivity() {
    private lateinit var usernameEditText: EditText
    private lateinit var saveButton: Button
    private lateinit var savedUsernameTextView: TextView
    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        usernameEditText = findViewById(R.id.usernameEditText)
        saveButton = findViewById(R.id.saveButton)
        savedUsernameTextView = findViewById(R.id.savedUsernameTextView)

        sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)

        // Load saved username and display it
        val savedUsername = sharedPreferences.getString("username", null)
        if (savedUsername != null) {
            savedUsernameTextView.text = savedUsername
        } else {
            savedUsernameTextView.text = "No username saved"
        }

        // Save username on button click
        saveButton.setOnClickListener {
            val username = usernameEditText.text.toString()
            if (username.isNotBlank()) {
                sharedPreferences.edit().putString("username", username).apply()
                savedUsernameTextView.text = username
            }
        }
    }
}

This code uses SharedPreferences to save and load a username.

When the screen opens, it reads the saved username from SharedPreferences. If there is one, it shows it; otherwise, it shows a default message.

When the user types a username and taps Save, the app saves the username in SharedPreferences and updates the displayed saved username immediately.

This way, the username stays saved even if the app is closed and reopened.

Final Result
Completed Screen
-------------------------
|       Settings        |
|-----------------------|
| Enter username:       |
| [Alice__________]     |
|                       |
| [ Save ]              |
|                       |
| Saved username:       |
| Alice                 |
-------------------------
User types a name in the input box
User taps Save button
The typed name is saved and shown below
When reopening the screen, the saved name is shown automatically
Stretch Goal
Add a Clear button that removes the saved username from SharedPreferences and updates the display
💡 Hint
Use sharedPreferences.edit().remove("username").apply() and update the TextView to show 'No username saved'