how can i make textviewtext persistent in android?

You can use SharedPreference to save it persistently

view.timeButton.setOnClickListener {
            val cal = Calendar.getInstance()
            val timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
                cal.set(Calendar.HOUR_OF_DAY, hour)
                cal.set(Calendar.MINUTE, minute)

                val simpleDateFormat = SimpleDateFormat("HH:mm")
                val date = simpleDateFormat.format(cal.time)
                alarmText.text = ("Texts at " + date)

                val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
                preferences.edit().putString("mydate", date).apply();
                
            }
            TimePickerDialog(
                context,
                timeSetListener,
                cal.get(Calendar.HOUR_OF_DAY),
                cal.get(Calendar.MINUTE),
                false
            ).show()
        }

And in your app’s onCreate retrieve the stored value

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
        try {
            val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
            val simpleDateFormat = SimpleDateFormat("HH:mm")
            val date: Date = simpleDateFormat.parse(preferences.getString("mydate", ""))
            alarmText.text = ("Texts at " + date)
        }
        catch (e: ParseException) {
        }
    }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top