'How to check status of Remove Animations accessibility setting programmatically
Solution 1:[1]
I have checked and when this value is toggled 3 values are modified:
//global settings
animator_duration_scale=0
transition_animation_scale=0
window_animation_scale=0
You can programmatically determine these by checking:
if (Settings.Global.getFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE) == 0f)
// Animations Off behaviour
else
// Animations On behaviour
I made a method that checks all three are not 0 (since all 3 would be aligned, and I think that these can sometimes be toggled elsewhere):
fun animationsEnabled(): Boolean =
!(Settings.Global.getFloat(contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE) == 0f
&& Settings.Global.getFloat(contentResolver, Settings.Global.TRANSITION_ANIMATION_SCALE) == 0f
&& Settings.Global.getFloat(contentResolver, Settings.Global.WINDOW_ANIMATION_SCALE) == 0f)
Remember these are scaling numbers so they are not just 0 and 1! That's why we need to get their float values and also why we check against 0. Then we need to remember that they will ALL be set to 0 if animations are off.
Sources:
Solution 2:[2]
One note about the above solution provided by quintin-balsdon:
it is important to use the version of Settings.Global.getFloat
that takes a default value, as it appears that (on emulators at least) if 'Remove animation' has never been switched on on a given device, then (at least) the setting for key Settings.Global.ANIMATOR_DURATION_SCALE
won't exist, and this code will result in an exception.
I'd therefore suggest:
fun Context.animationsEnabled(): Boolean =
!(Settings.Global.getFloat(contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f) == 0f
&& Settings.Global.getFloat(contentResolver, Settings.Global.TRANSITION_ANIMATION_SCALE, 1.0f) == 0f
&& Settings.Global.getFloat(contentResolver, Settings.Global.WINDOW_ANIMATION_SCALE, 1.0f) == 0f)
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 | Tom Gilbert |