Why SwitchPreference is not showing animation when switching from on to off & vice versa?
Asked Answered
M

3

8

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>
Meredeth answered 24/11, 2015 at 7:44 Comment(4)
The same problem here. One of my SwitchPreferences behaves this way. It's strange because only 1 of 5 preferences doesn't have an animation. The xml file is located in xml-v21.Badajoz
To expand on this, I'm seeing a similar thing when multiple switches and checkboxes are in a preference screen. The last checkbox or switch does not animate in my project. It works in a sample project. Both activities (sample and my project) use the same theme and files. I cannot at all understand why this happensOblique
Yeah I also want to know that for me it's the same :/Wyon
Same for me. It was working fine, all checkboxes were animating. Then I changed something elsewhere (well, I changed a lot of things) and suddenly one of the checkboxes stopped animating... I tried to revert the changes but with no luck.Aforethought
T
3

Causes

I noticed that a few different things could cause the animation to go missing for my SwitchPreference objects:

  1. if the SwitchPreference is the very first Preference in the settings activity.

  2. 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 first Preference in the PreferenceScreen instead. Examples below.

DummyPreference.java

public class DummyPreference extends Preference
{
    public DummyPreference(Context context,AttributeSet attrs)
    {
        super(context,attrs);
    }

    @Override
    public View getView(View convertView,ViewGroup parent)
    {
        View v = new View(getContext());
        v.setVisibility(View.GONE);
        return v;
    }
}

pref_whatever.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <com.exaple.DummyPreference/>
    <!-- other preference controls here -->
</PreferenceScreen>

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 the Activity or Fragment containing the Preference objects.

I know this is an old post. I'm just hoping that it may help someone else in the future.

Triangle answered 12/6, 2016 at 23:53 Comment(0)
B
0

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.

@Override
public void setChecked(boolean checked) {
    // Always persist/notify the first time; don't assume the field's default of false.
    final boolean changed = mChecked != checked;
    if (changed || !mCheckedSet) {
        mChecked = checked;
        mCheckedSet = true;
        persistBoolean(checked);
        if (changed) {
            notifyDependencyChange(shouldDisableDependents());
            //notifyChanged();  *******THIS IS REDRAWING THE VIEW IMMEDIATELY
        }
    }
}
Bughouse answered 19/1, 2017 at 12:7 Comment(0)
F
0

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:

<com.test.MyPreference>
<!--can define custom_switch_layout.xml base on preference_material.xml-->
android:layout="@layout/custom_switch_layout"
android:key="new_switch"
android:title="pref_switch_title"</com.test.MyPreference>
Film answered 4/9, 2017 at 10:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.