Osmdroid Night-Mode with Preferences

125 Views Asked by At

So I want to make a CheckBoxPreference to turn on the Night-Mode in osmdroid.But the code doesn't to anything. I made the same thing without PreferenceScreen,a normal LinearLayout with a switch, it worked well, but I wanted to use the preference. Does someone know, what's wrong?


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.allgemein);

        final SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.gruppen_app", Context.MODE_PRIVATE);


        @SuppressLint("ResourceType") final CheckBoxPreference night_mode = (CheckBoxPreference) findPreference(this.getResources()
                .getString(R.id.night_mode_btn));

        night_mode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if(night_mode.isChecked()) {
                    sharedPreferences.edit().putBoolean("isChecked", true).apply();
                    map.getOverlayManager().getTilesOverlay().setColorFilter(TilesOverlay.INVERT_COLORS);
                }else{
                    sharedPreferences.edit().putBoolean("isChecked", false).apply();
                }

                return false;
            }
        });




    }
}
1

There are 1 best solutions below

0
Alex Rmcf On
@SuppressLint("ResourceType") final CheckBoxPreference night_mode = (CheckBoxPreference) findPreference(this.getResources()
                .getString(R.id.night_mode_btn));

R.id return int, not string.

Here:

public boolean onPreferenceChange(Preference preference, Object newValue)

you can do this:

boolean isChecked = Boolean.parseBoolean(String.valueOf(newValue));

and then just:

sharedPreferences.edit().putBoolean("isChecked", isChecked).apply();