Ad

How To Change Toolbar Color According To The Radio Button Selected After The Second Login Using Android Kotlin

- 1 answer

I need to change the toolbar color in the second login time according to the RadioButtonselected in the first login time in android kotlin. I've used sharedPreferences to get the value of the radioButton selected in the first login.

To set the radio button value in sharedPreferences at the first login time, I've used the following code

fun setSharedPreferences() {
        val sharedPreferences = getSharedPreferences(myPreferences, Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.putString("Selected Value",radioButtonValue)
        editor.apply()
    }

In the Second login time, I've used the below method to get the value from shared preferences that was already set at the first login time.

fun getSharedPreferences(): String {
         val prefs = getSharedPreferences(myPreferences, Context.MODE_PRIVATE)
         restoredValue= prefs.getString("text", devMode)
         return restoredValue
}

This the code that I've used to change the color of toolbar in the second login time

 val restoredText=getSharedPreferences()
 if(restoredText==="selectedRadioButtonValue")
 {
   toolbar.setBackgroundColor(Color.parseColor("#FF0000"));
 }
}
Ad

Answer

first of all in your getSharedPreferences method you try to receive different key value than you set on the first login (you are saving first login state in Selected Value key and try to receive from different key which is text)

second for your answer try below code

val restoredText=getSharedPreferences()
 if(restoredText.equals("selectedRadioButtonValue"))
 {
   toolbar.setBackgroundDrawable(ColorDrawable(Color.parseColor("#YOUR_COLOR_CODE")))
 }
}
Ad
source: stackoverflow.com
Ad