I'm using Android Studio Electric Eel and I started with a new "Basic Activity" project. It comes with a light and dark theme, /res/values/theme.xml /res/values-night/theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.Example" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">@color/md_theme_light_primary</item>
</style>
</resources>
I understand I need to use this code:
val typedValue = TypedValue()
view.context.theme.resolveAttribute(R.attr.colorPrimary, typedValue, true)
val colorPrimary = typedValue.data
And that works almost.
For whatever reason, R.attr.colorPrimary
won't resolve off of the default com.example.project.R
that is my local project. When I type R.attr.colorPrimary
it only auto-complete resolves to com.google.android.material.R.attr.colorPrimary
(it also can resolve to some androidx components, that also work)
When I use that, it does work, and it does use my colors.xml pointed to from my theme.xml. I just want to understand why it won't simply resolve from my theme.xml. In all the examples I found online it was named styles.xml so I renamed it to that, and everything worked identically, no change.
When I ctrl-click the resolved color it takes me to a cached file in ...\transformed\appcompat-1.6.1\res\values\values.xml that's <attr format="color" name="colorPrimary"/>
And if I ctrl-click the parent="Theme.MaterialComponents.DayNight.DarkActionBar"
it takes me to a cache of ...\transformed\material-1.8.0\res\values\values.xml
I just want to understand why. It seems my colors still override the default ones and it works using the com.google.android.material.R
. In xml layouts ?attr/colorPrimary
works just fine. But in nowhere that I looked did it say that when programmatically resolving it needed that path.