Waza_Be
Waza_Be

Reputation: 39556

Is there a way to programmatically get the Android Q System color accent

As you noticed, Google has included a way to select a system wide color accent in the Android settings:

enter image description here

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.

enter image description here

Upvotes: 6

Views: 1069

Answers (1)

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

Related Questions