Reputation: 39556
As you noticed, Google has included a way to select a system wide color accent in the Android settings:
I first thought this would be impossible to use in my app, but Gboard succeed to use it to theme UI elements accordingly, as we can see in this news: https://9to5google.com/2019/05/08/android-q-gboard-accent-color/
I was then wondering if this is a public API, or of there is a way to get it from one way or another.
Thanks for any idea.
Upvotes: 6
Views: 1069
Reputation: 1125
You have to use Theme.DeviceDefault
theme as source which independent from AppCompat
world.
I tested the follow snippet and it works fine when I changed the accent color on Android Q:
@ColorInt
fun getDeviceAccentColor(context: Context) : Int {
val value = TypedValue()
val ctx = ContextThemeWrapper(context, android.R.style.Theme_DeviceDefault)
ctx.theme.resolveAttribute(android.R.attr.colorAccent, value, true)
return value.data
}
Upvotes: 5