'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 1:[1]
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:
Solution 2:[2]
Okay. I did some intensive research and trial and error. After that I found out, that I could access a private property @*android:color/accent_device_default_light
. BUT this is just possible if you change your parent class for the activity from AppCompatActivity
to Activity
because the AppCompat can't set up the toolbar with this private property. Further it's not recommended to use private properties because they are likely to get deleted or changed in the future.
Solution 3:[3]
@color/colorAccent use this ,it's default color in android or you can customize your color in the color.xml
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Tarik Weiss |
Solution 3 | rachna |