0

I have two images (not colors) called black and white. I've written this code so far:

public class MainActivity extends AppCompatActivity
{
    View parentView;
    SwitchCompat switchCompat;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        switchCompat = findViewById(R.id.switchCompat);
        parentView = findViewById(R.id.parentView);

        SharedPreferences prefs = getSharedPreferences("save", MODE_PRIVATE);
        switchCompat.setChecked(prefs.getBoolean("value", false);
        
        switchCompat.setOnClickListener(view -> {
            if(switchCompat.isChecked()) {
               parentView.setImageResource(R.drawable.black);
               SharedPreferences.Editor ed = getSharedPreferences("save", MODE_PRIVATE);
               ed.putBoolean("value", true);
               ed.apply();
               switchCompat.setChecked(true);
            } else {
               parentView.setImageResource(R.drawable.white);
               SharedPreferences.Editor ed = getSharedPreferences("save", MODE_PRIVATE);
               ed.putBoolean("value", false);
               ed.apply();
               switchCompat.setChecked(false);
            }
        });
    }

Default background is white. As you see, if the user check switch on the background should be black, but this doesn't work. Do you know any solution?

Wagner_91
  • 3
  • 3
  • Does this answer your question? [Android: checkbox listener](https://stackoverflow.com/questions/8386832/android-checkbox-listener) – cutiko Nov 16 '21 at 17:46
  • Sorry, but no. I can save switch instance state but i cannot change background. – Wagner_91 Nov 16 '21 at 17:49
  • yes you can, is literally the answer someone gave you below, use `setOnCheckedChangeListener` instead of click listener – cutiko Nov 16 '21 at 18:35

1 Answers1

0

You needn't set the SwitchCompat if its changed, its redundant. Also SharedPreferences.Editor was initialized wrong. Try this

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    View parentView;
    SwitchCompat switchCompat;
    SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        switchCompat = findViewById(R.id.switchCompat);
        parentView = findViewById(R.id.parentView);

        prefs = getSharedPreferences("save", MODE_PRIVATE);
        switchCompat.setChecked(prefs.getBoolean("value", false));
        if (prefs.getBoolean("value", false)) { 
            parentView.setBackgroundResource(R.drawable.black);
         } else {
            parentView.setBackgroundResource(R.drawable.white);
         }
        switchCompat.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(buttonView.getId() == R.id.switchCompat) {
                if(isChecked) {
                  parentView.setImageResource(R.drawable.black);
                  SharedPreferences.Editor ed = prefs.edit();
                  ed.putBoolean("value", true);
                  ed.apply();
                } else {
                  parentView.setImageResource(R.drawable.white);
                  SharedPreferences.Editor ed = prefs.edit();
                  ed.putBoolean("value", false);
                  ed.apply();
                }
        }
    }
}
  • Thanks for answer. I tried this and I can change the background, but when I exit activity it doesn't save the background change. – Wagner_91 Nov 16 '21 at 18:42
  • You need to call `parentView.setImageResource(R.drawable.black);` or `parentView.setImageResource(R.drawable.white);` in `onCreate` based on `prefs.getBoolean("value", false)`. Add a check in `onCreate` with boolean from shared prefs and set black or white accordingly – Sidharth Shambu Nov 16 '21 at 18:57
  • Do you think this? if (prefs.getBoolean("value", false)) { parentView.setBackgroundResource(R.drawable.white); } else { parentView.setBackgroundResource(R.drawable.black); } – Wagner_91 Nov 16 '21 at 19:18
  • I have updated this in the snippet above. It sets black if 'prefs.getBoolean("value", false)' returns true, else sets white. – Sidharth Shambu Nov 16 '21 at 19:45
  • Thanks legend! It's working perfect now! – Wagner_91 Nov 16 '21 at 20:29