How to create a Theme instance in android

Viewed 122

There are some methods in android, for example getColor of Resource class, which ask for a Theme parameter. I didn't find a way how to create a Theme instance and therefore don't know what to send to getColor method as a Theme. I know that null is an option but I prefer to understand what can I do with the parameter (the meaning of Theme parameter in getColor method)

1 Answers

If you're just referencing fixed colors from resource xml you won't notice a difference.

However you can use getColor to reference a color selector too, for example:

color/singlecolor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/colorPrimary" />
</selector>

In this case there's reference to themed style attribute colorPrimary.

  • if you pass null as theme you will receive value from your applications main theme
  • if you pass context.getTheme you will receive value from your activitys theme

Of course your activity needs to have a custom theme that has different colorPrimary than applications theme itself to notice the difference.

Related