Search code examples
androidandroid-themeandroid-stylesandroid-10.0

Get Android system accent color (Android 10 System color accent)


This question should be simple, but i didn't find an answer. I have an app with selectable accent, and i'm trying to add an option to use the android system accent (apps like Lawnchair have such an option). In the style for the system accent, i tried to get this accent in every way possible:

?android:colorAccent

?android:attr/colorAccent

?attr/colorAccent

And this is the style:

<style name="AppTheme.systemAccent" parent="AppTheme">
    <item name="colorAccent">???</item>
</style>

Nothing seems to work and the app crashes, but i'm certain that this is possible. The accent selection, when i use normal colors, works fine. Where am i wrong?

EDIT: Just to be clear, i'm trying to get the system accent color, i.e. the system wide color used in settings, notification panel and so on. This color is now selectable in Android 10 and was selectable before in rom like the Oxygen OS. Let's say i select a red accent in settings->customization on my Android 10 device. I want to get this red accent in my app


Solution

  • You can get it programmatically:

    TypedValue typedValue = new TypedValue();
    ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this,
         android.R.style.Theme_DeviceDefault);
    contextThemeWrapper.getTheme().resolveAttribute(android.R.attr.colorAccent,
         typedValue, true);
    int color = typedValue.data; 
    

    Just to be clear I am referring to the Android Q System color accent:

    enter image description here enter image description here