Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Enable/disable two more SwitchPreference when one switch Preference is enabled

Posted on November 11, 2022 By

Solution 1 :

To do this, you need some fragment class to manage your preferences in code.

public class PreferenceFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.your_xml, rootKey);
        
        final SharedPreferences preferences = androidx.preference.PreferenceManager.getDefaultSharedPreferences(getActivity());

        // dont forget to check nullity in real app production, findPreference could return null
        final SwitchPreferenceCompat switchPreference1 = (SwitchPreferenceCompat) findPreference("key1");
        final SwitchPreferenceCompat switchPreference2 = (SwitchPreferenceCompat) findPreference("key2");
        final SwitchPreferenceCompat switchPreference3 = (SwitchPreferenceCompat) findPreference("key3");
        if (preferences.getBoolean("key1", false)) {
            switchPreference2.setEnabled(true);
            switchPreference3.setEnabled(true);
        } else {
            switchPreference2.setEnabled(false);
            switchPreference3.setEnabled(false);
        }
        switchPreference1.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                boolean turned = (Boolean) newValue;
                if (turned) {
                    switchPreference2.setEnabled(true);
                    switchPreference3.setEnabled(true);
                } else {
                    switchPreference2.setEnabled(false);
                    switchPreference3.setEnabled(false);
                }
                preferences.edit().putBoolean("key1", turned).apply();
                return true;
            }
        });

    }
}

@EDIT
You can override an SwitchPreferenceCompat class to make an enabled/disabled effect

public class CustomSwitchPreference extends SwitchPreferenceCompat {

    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle, int defStyleRes) {
        super(context, attrs, defStyle, defStyleRes);
    }

    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomSwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSwitchPreference(Context context) {
        super(context, null);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        TextView title = (TextView) holder.findViewById(android.R.id.title);
        TextView summary = (TextView) holder.findViewById(android.R.id.summary);
        if (title.isEnabled()) {
            title.setTextColor(Color.BLACK);
        } else {
            title.setTextColor(Color.GRAY);
        }

        if (summary.isEnabled()) {
            summary.setTextColor(Color.BLACK);
        } else {
            summary.setTextColor(Color.GRAY);
        }
    }
}

In that case your preference xml should look like this

<PreferenceCategory
    android_title="Category"
    app_iconSpaceReserved="false"
    >

    <com.test.CustomSwitchPreference
        android_defaultValue="false"
        android_key="key1"
        android_title="title1"
        />

    <com.test.CustomSwitchPreference
        android_defaultValue="false"
        android_key="key2"
        android_title="title2"
        />

    <com.test.CustomSwitchPreference
        android_defaultValue="false"
        android_key="key3"
        android_title="title3"
        />

</PreferenceCategory>

And you should change an access to the preference in fragment like

    final CustomSwitchPreference switchPreference1 = (CustomSwitchPreference) findPreference("key1");
    final CustomSwitchPreference switchPreference2 = (CustomSwitchPreference) findPreference("key2");
    final CustomSwitchPreference switchPreference3 = (CustomSwitchPreference) findPreference("key3");

Problem :

When one preference is selected, other two preferences should be available for clicks, otherwise the should be shady/dim so that the user knows they are not clickable. Like how we set alpha for text.
Attached the code below. Please Let me know if there is a solution.

<PreferenceCategory
        android_title="@string/category_dnd_title"
        app_iconSpaceReserved="false">

        <androidx.preference.SwitchPreferenceCompat
            android_defaultValue="false"
            android_key="key1"
            android_title="title1" />

        <androidx.preference.SwitchPreferenceCompat
            android_defaultValue="false"
            android_dependency="key1"
            android_key="key2"
            android_title="title2" />

        <androidx.preference.SwitchPreferenceCompat
            android_defaultValue="false"
            android_dependency="key1"
            android_key="@key3"
            android_title="title3"/>
    </PreferenceCategory>

Comments

Comment posted by Harish Dadi

Hey, I have tried it but I don’t see anything different from adding a dependent. I am unable to access the other two preferences yes, but the shady/dim of the text part I am not getting

READ  [FIXED] java - Is there a way to see how long was the response from Firebase?
Powered by Inline Related Posts

Comment posted by grabarz121

Which api do you test? By default this should be gray, when is disabled. Maybe you have some style that makes it not working.

Comment posted by Harish Dadi

The toggle is becoming grey, but the title is still bold. In my use case I have ListPreference, there the title is always bold.

Comment posted by grabarz121

Try to override an

Android Tags:android, android-preferences, switchpreference

Post navigation

Previous Post: [FIXED] android – How to pass data by Back Button, Kotlin
Next Post: [FIXED] Project with external module issue in android studio 3.6

Related Posts

[FIXED] android – Open bottom sheet dialog only for one time Android
[FIXED] How to add a delay in For loop in kotlin android Android
[FIXED] java – Call void method from another method in the same class Android
[FIXED] android – Can’t use Timber but have implemented the dependency Android
[FIXED] java – Fragment doesn’t get value from ViewModel Android
[FIXED] android – Making call from PC via bluetooth connected mobile phone Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme