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.