Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

like image 411
Waza_Be Avatar asked May 10 '19 07:05

Waza_Be


People also ask

What is accent color in Android Studio?

Accent color This helps accented elements stand out. Use accent colors sparingly to accent key elements, such as text or buttons.

What is primary and accent color Android?

The primary color is the color displayed most frequently across your app's screens and components. The primary variant color is used to distinguish two elements of the app using the primary color, such as the top app bar and the system bar. The secondary color provides more ways to accent and distinguish your product.

What is accent color in app?

The accent color is used more subtly throughout the app, to call attention to key elements. The resulting juxtaposition of a tamer primary color and a brighter accent, gives apps a bold, colorful look without overwhelming the app's actual content. Save this answer.


1 Answers

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
}
like image 198
János Sicz-Mesziár Avatar answered Oct 10 '22 19:10

János Sicz-Mesziár