0

Is it possible to do something like

var myThemeVariable = "redTheme" //could be either redTheme, greenTheme, or blueTheme for example
context.setTheme(R.style.myThemeVariable) //sets theme to R.style.redTheme

instead of what i usually do

var myThemeVariable = "redTheme" //could be either redTheme, greenTheme, or blueTheme
when (myThemeVariable) 
{
"redTheme" -> context.setTheme(R.style.redTheme)
"greenTheme" -> context.setTheme(R.style.greenTheme)
"blueTheme" -> context.setTheme(R.style.blueTheme)
}

In this example it's not super cluttered, but in my actual code there's a lot more. My current solution is not only hard to understand, add to, or remove from; it's also (I imagine) unnecessarily computationally expensive. Is something akin to the first approach possible? If it's not, does any language have something like it? Thanks!

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Marvil
  • 17
  • 4
  • var myThemeVariable = R.style.blueTheme context.setTheme(myThemeVariable) – blackapps May 12 '22 at 03:40
  • But better convert name to resource id: https://stackoverflow.com/questions/3476430/how-to-get-a-resource-id-with-a-known-resource-name – blackapps May 12 '22 at 03:49
  • setTheme(int themeId);<- it requires an integer id not a string so you can definitely store an id as an int and pass that variable to this method. – Haseeb Hassan Asif May 12 '22 at 05:30

2 Answers2

0

Thanks y'all, in the end I searched about @blackapps' and @Haseeb Hassan Asif's recommendations and found this post so I ended up doing this :)

val themeResID = context.resources.getIdentifier((sharedPreferences.getString("theme", "default_standard")), "style", context.packageName) //get theme from shared preferences
context.setTheme(themeResID)
Marvil
  • 17
  • 4
0

getIdentifier uses reflection under the hood, so it has poor performance. If you're using it sparingly, like to set the theme once whenever a button is pressed, that performance difference will not be noticeable. But the code is overly complicated either way.

Just use your IDs directly like this. No Strings or when statements are necessary.

var myThemeVariable = R.style.redTheme // redTheme, greenTheme, or blueTheme

context.setTheme(myThemeVariable)
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • The problem is that the myThemeVariable is a string, and the way I have it set up, it's not practical to change it. I only use it once per activity before drawing, so It'll probably be okay. – Marvil May 12 '22 at 14:04
  • Yeah, that will be fine. Just for future use, this will keep your code simpler. Strings are not usually the best way to store your application state in general because they make code more complicated. And if you scale it way up, they use a lot more memory. For instance, the String `"redTheme"` probably uses about 36 bytes whereas an Int would use 4 bytes. Of course, on modern devices, you would have to be using many, many Strings for the memory to really become a consideration. – Tenfour04 May 12 '22 at 14:11