Search code examples
javaandroidsharedpreferencesandroid-switch

How to programmatically set switch to unchecked depending on the switch in PreferenceActivity?


I have a switch on the Tab in mainActivity and a switch in Preference which should be interconnected. The switch in the settings should set the same position in the second switch on the Tab. The code works to switch on, but doesnt work back (to switch off). I understand that nothing happens when switch is off and I need a listener, but I don’t know how to use it correctly How to fix the problem?

settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreference
            android:key="pref_details"
            android:title="@string/details"
            android:summary="@string/details_summary"
            android:defaultValue="false">
    </SwitchPreference>
</PreferenceScreen>

MainActivity.xml:

<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Switch
        android:id="@+id/sw_pr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/textView17"
        android:textStyle="bold" />
</androidx.core.widget.NestedScrollView>

MainActivity.java:

public class  MainActivity extends AppCompatActivity {
    public static Boolean storeDet;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this);
        storeDet = shp.getBoolean("pref_details", false);
    ...
    }
    ... 
}

Tab1.java:

public class Tab1 extends Fragment {
    Switch sw_pr;
    Boolean sw_det;
    Boolean sw_det_flag;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        sw_pr = rootView.findViewById(R.id.sw_pr);

        // Attempt to determine switch value from Preferences: 
        sw_det = Boolean.parseBoolean(String.valueOf(MainActivity.storeDet));
        if (sw_det = true) {
            sw_pr.setChecked(true);
        } else {
            sw_pr.setChecked(false);
        }
        ...

        // Tab switch listener:
        sw_pr.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    res_new_pr.setVisibility(View.VISIBLE);
                } else {
                    res_new_pr.setVisibility(View.GONE);
                }
            }
        });
    }
        ...
}

Solution

  • No need to use = to compare boolean value. Use like below:

    sw_det = MainActivity.storeDet;
    if (sw_det) {
        sw_pr.setChecked(true);
    } else {
        sw_pr.setChecked(false);
    }