I tryed to save the string text of a button (id=language) but when the app starts the text become empty… can you help me?
Here the code:
package com.randomnumbers.dice
import android.content.Context
import android.media.MediaPlayer
import android.os.*
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.random.Random
class MainActivity() : AppCompatActivity() {
private lateinit var mp: MediaPlayer
private lateinit var mp2: MediaPlayer
private lateinit var mp3: MediaPlayer
private lateinit var mp4: MediaPlayer
private lateinit var mp5: MediaPlayer
private var totalTime: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val pref = getPreferences(Context.MODE_PRIVATE)
val Language = pref.getString("Language", "")
language.setText(Language)
mp = MediaPlayer.create(this, R.raw.suono1)
mp.isLooping = false
mp.setVolume(0.5f, 0.5f)
totalTime = mp.duration
mp2 = MediaPlayer.create(this, R.raw.suono2)
mp2.isLooping = false
mp2.setVolume(0.5f, 0.5f)
totalTime = mp2.duration
mp3 = MediaPlayer.create(this, R.raw.suono3)
mp3.isLooping = false
mp3.setVolume(0.5f, 0.5f)
totalTime = mp3.duration
mp4 = MediaPlayer.create(this, R.raw.suono4)
mp4.isLooping = false
mp4.setVolume(0.5f, 0.5f)
totalTime = mp4.duration
mp5 = MediaPlayer.create(this, R.raw.suono5)
mp5.isLooping = false
mp5.setVolume(0.5f, 0.5f)
totalTime = mp5.duration
val rollButton = findViewById<Button>(R.id.rollButton)
val resultsTextView = findViewById<TextView>(R.id.resultsTextView)
val seekBar = findViewById<SeekBar>(R.id.seekBar)
val language = findViewById<Button>(R.id.language)
val Biscotto = findViewById<TextView>(R.id.Biscotto)
val button = findViewById(R.id.rollButton) as Button
val textView = findViewById<TextView>(R.id.textView)
val dadoN1 = findViewById<ImageView>(R.id.dadoN1)
val dadoN2 = findViewById<ImageView>(R.id.dadoN2)
val dadoN3 = findViewById<ImageView>(R.id.dadoN3)
val dadoN4 = findViewById<ImageView>(R.id.dadoN4)
val dadoN5 = findViewById<ImageView>(R.id.dadoN5)
val dadoN6 = findViewById<ImageView>(R.id.imageView11)
val dadoN7 = findViewById<ImageView>(R.id.imageView13)
val dadoN8 = findViewById<ImageView>(R.id.imageView12)
val dadoN9 = findViewById<ImageView>(R.id.imageView14)
val dadoN10 = findViewById<ImageView>(R.id.imageView15)
language.setOnClickListener {
if ("EN" == language.text) {
language.text = "IT"
rollButton.text = "Roll the dice"
textView.text = "choose the number of dice:"
if ("Lancia il dado" == Biscotto.text) {
Biscotto.text = "Roll the dice"
}
} else if ("IT" == language.text) {
language.text = "EN"
rollButton.text = "Lancia il dado"
textView.text = "imposta il numero di dadi:"
if ("Roll the dice" == Biscotto.text) {
Biscotto.text = "Lancia il dado"
}
fun onSave (view: View) {
}
}
val pref = getPreferences (Context.MODE_PRIVATE)
val editor = pref.edit()
editor.putString("Language", language.text.toString())
editor.commit()