Search code examples
androidkotlintextviewsharedpreferences

Remember TextView size after changing it with Shared Preferences


I'm creating a songbook app, and I want to be able to zoom in and out the lyrics of songs. I added zoom functions but I want the app to be able to remember what text size the lyrics had before the user left the app. I tried using Shared Preferences but it isn't working.

Here is what I tried so far

class Cantare : AppCompatActivity() {

    lateinit var tvSizePrefs: SharedPreferences
    lateinit var tvSizeEditor: SharedPreferences.Editor
    var mTextSize: Float = 50f

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

        tvSizePrefs = getSharedPreferences("TextSizePref", Context.MODE_PRIVATE)
        tvSizeEditor = tvSizePrefs.edit()

        setSupportActionBar(findViewById(R.id.toolbar2))
        supportActionBar?.setDisplayShowTitleEnabled(false)

        val songTitle = findViewById<TextView>(R.id.songTitle)
        val songString = findViewById<TextView>(R.id.songString)

        songString.textSize = tvSizePrefs.getFloat("TextSize", 50f)

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_song, menu)


        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        val id: Int = item.itemId

        when(id) {
            R.id.zoomOut -> {
                mTextSize -= 2f
                songString.setTextSize(TypedValue.COMPLEX_UNIT_PX, (mTextSize))
                tvSizeEditor.putFloat("TextSize", mTextSize)
                tvSizeEditor.apply()
                Toast.makeText(this, songString.textSize.toString(), Toast.LENGTH_SHORT).show()
            }
            R.id.zoomIn -> {
                mTextSize += 2f
                songString.setTextSize(TypedValue.COMPLEX_UNIT_PX, (mTextSize))
                tvSizeEditor.putFloat("TextSize", mTextSize)
                tvSizeEditor.apply()
                Toast.makeText(this, songString.textSize.toString(), Toast.LENGTH_SHORT).show()
            }
        }

        return super.onOptionsItemSelected(item)
    }

}

Solution

  • I see two problem with your code.
    First one is the mTextSize always gets initialized with 50f and you are not setting its value from saved text size value. Only set the size of your text view inside onCreate. So mTextSize always starts from 50f and when you zoom in/out for the first time, size of the texts resets back to ~50f.
    The second one is you are not passing complex unit when setting text view's size in your onCreate.
    I think changing your code to something like this will solve your problem:

    override fun onCreate(savedInstanceState: Bundle?) {
        // ...
        // replace songString.textSize = tvSizePrefs.getFloat("TextSize", 50f) with:
        mTextSize = tvSizePrefs.getFloat("TextSize", 50f)
        text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize)
        // ...
    }