0
0
Android Kotlinmobile~20 mins

Why local storage enables offline access in Android Kotlin - Build It to Prove It

Choose your learning style9 modes available
Build: Offline Notes
This screen allows users to save notes locally on their device so they can access them even without internet connection.
Target UI
-------------------------
|      Offline Notes     |
|-----------------------|
| [Enter note here...]   |
|                       |
| [Save Note]            |
|                       |
| Saved Notes:           |
| - Note 1               |
| - Note 2               |
|                       |
-------------------------
A TextField for entering a note
A Save button to store the note locally
A list showing all saved notes from local storage
Notes must persist and be visible even when the app restarts or device is offline
Starter Code
Android Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
import android.widget.ListView

class OfflineNotesActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_offline_notes)

        val noteInput = findViewById<EditText>(R.id.noteInput)
        val saveButton = findViewById<Button>(R.id.saveButton)
        val notesList = findViewById<ListView>(R.id.notesList)

        // TODO: Load saved notes from local storage and display

        saveButton.setOnClickListener {
            val note = noteInput.text.toString()
            if (note.isNotEmpty()) {
                // TODO: Save note to local storage
                // TODO: Update list view with new note
                noteInput.text.clear()
            }
        }
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.ListView

class OfflineNotesActivity : AppCompatActivity() {
    private val PREFS_NAME = "offline_notes_prefs"
    private val NOTES_KEY = "notes"
    private lateinit var notesList: MutableList<String>
    private lateinit var adapter: ArrayAdapter<String>

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

        val noteInput = findViewById<EditText>(R.id.noteInput)
        val saveButton = findViewById<Button>(R.id.saveButton)
        val notesListView = findViewById<ListView>(R.id.notesList)

        // Load saved notes from SharedPreferences
        val prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
        val savedNotes = prefs.getStringSet(NOTES_KEY, emptySet()) ?: emptySet()
        notesList = savedNotes.toMutableList()

        // Setup adapter to display notes
        adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, notesList)
        notesListView.adapter = adapter

        saveButton.setOnClickListener {
            val note = noteInput.text.toString().trim()
            if (note.isNotEmpty()) {
                notesList.add(note)
                adapter.notifyDataSetChanged()

                // Save updated notes to SharedPreferences
                prefs.edit().putStringSet(NOTES_KEY, notesList.toSet()).apply()

                noteInput.text.clear()
            }
        }
    }
}

This app uses SharedPreferences to save notes locally on the device. When the screen starts, it loads any saved notes from SharedPreferences and shows them in a list. When the user types a note and taps Save, the note is added to the list and saved back to SharedPreferences. This way, notes stay saved on the device and can be accessed even without internet or after restarting the app. Local storage like SharedPreferences enables offline access by keeping data on the device itself.

Final Result
Completed Screen
-------------------------
|      Offline Notes     |
|-----------------------|
| [Enter note here...]   |
|                       |
| [Save Note]            |
|                       |
| Saved Notes:           |
| - Buy groceries        |
| - Call mom             |
|                       |
-------------------------
User types a note in the text field
User taps Save Note button
Note appears in the list below
Notes remain visible after app restart or offline
Stretch Goal
Add a feature to delete a note by tapping on it in the list
💡 Hint
Use ListView's setOnItemClickListener to detect taps and remove the tapped note from the list and SharedPreferences