I've made a SwitchPreference
for my app's preferences
.
The problem is that the SwitchPreference
is not showing animation when I'm switching between on & off, rather, it is switching with a sudden jerk.
Here's preferences.xml
file's code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:id="@+id/notification"
android:key="notification"
android:title="@string/notification"
android:defaultValue="true"/>
</PreferenceScreen>
I'm a beginner, so please cooperate & let me know what's wrong here.
Thanks in advance.
I had this problem too,and I had tried every solution noticed in stackoverflow but still can't solve the questions.It's seems an android bug when you try to extend subclass of Preference,the animation will disappear. See here for their bugtracker entry.
Finally I found the solution, I create a class extends Preference,and set layout which contain a switch component, and deal with the click event in custom class, the switch's animation works fine.
xml file mostly like:
Causes
I noticed that a few different things could cause the animation to go missing for my
SwitchPreference
objects:if the
SwitchPreference
is the very firstPreference
in the settings activity.if I extend the
SwitchPreference
and use that instead (post describing a similar problem).Fixes
To avoid the first problem, I created a
DummyPreference
class which I used as the firstPreference
in thePreferenceScreen
instead. Examples below.DummyPreference.java
pref_whatever.xml
To avoid the second problem, I had to just resort to using android's plain old
Preference
classes in the XML, and I moved any extra logic needed into theActivity
orFragment
containing thePreference
objects.I know this is an old post. I'm just hoping that it may help someone else in the future.
I had a similar problem with Checkboxes, I imagine it might be the same problem. The problem line seems to be under the parent class, TwoStatePreference's setChecked() function. The notifyChanged() line is actually telling the preference to redraw itself, which means it will suddenly appear as checked. You can extend SwitchPreference and just override that function and comment that line out, it should work just fine, as long as you don't anything else in that preference to redraw itself.