Home > other >  How to change textview font size in recyclerView in Kotlin?
How to change textview font size in recyclerView in Kotlin?

Time:01-05

How to change textView font size in RecyclerView (from shared preferences)?

I tried to save size in SaveData class:

class SaveData(context: Context) {
    private val sharedPreferences: SharedPreferences =
        context.getSharedPreferences("file", Context.MODE_PRIVATE)

fun setFontSize(size: Float?) {
        val editor = sharedPreferences.edit()
        editor.putFloat("FontSize", size!!)
        editor.apply()
    }

    fun loadFontSize(): Float {
        val size = sharedPreferences.getFloat("FontSize", 0f)
        return size
    }
}

What is the code and where put it in Adapter or Activity?

CodePudding user response:

ok I am guessing that you already have some mechanism to store the font size in shared pref. Now, you just need to ask sharedPref from recycler view adapter. To access sharedPref you can use itemView.getContext()(in java) or itemView.context(in kotlin). Now you have context(lets say you named it ctx), you can do this.

val pref= SaveData(ctx)
val fontSize= pref.loadFontSize()

Hope this works.

  • Related