0

hello guys i want to change my checked text view to switch preference what methods should i use this is my checked text view method i want to do the same thing but with switch preference

final CheckedTextView cc = (CheckedTextView) findViewById(R.id.checkedTextView);
    if (!mysetting.service) {
        ctv.setChecked(false);
    } else if (mysetting.service) {
        ctv.setChecked(true);
    }
    ctv.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (ctv.isChecked()) {
                mysetting.service = false;
                ctv.setChecked(false);
                Editor editor = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
                editor.putBoolean("service", false);
                editor.commit();
                return;
            }
            ctv.setChecked(true);
            mysetting.service = true;
            editor = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
            editor.putBoolean("service", true);
            editor.commit();
        }
Rob
  • 2,243
  • 4
  • 29
  • 40
fahd mana
  • 13
  • 6

2 Answers2

0

It's highly recommended to use setOnCheckedChangeListener to turn on (check) or turn off (uncheck), once this view is a CompoundButton (link):

    switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(ischecked){
      //do something if is checked
    }else{
     //do something if is unchecked
    }
}

});

Natan Felipe
  • 173
  • 1
  • 5
0
private boolean serviceEnable = false;

    public boolean isServiceEnable() {
        return serviceEnable;
    }

    public void setServiceEnable(boolean serviceEnable) {
        this.serviceEnable = serviceEnable;
    }

    private void savePreference() {
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
        editor.putBoolean("service", isServiceEnable());
        editor.commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.checked_textview);

        final CheckedTextView cc = (CheckedTextView) findViewById(R.id.check_text_view);
        cc.setChecked(isServiceEnable());

        cc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setServiceEnable(!isServiceEnable());
                cc.setChecked(isServiceEnable());
                savePreference();
            }
        });
    }

I hope it can help you

Dungnbhut
  • 176
  • 5
  • thanks for help,but what i want is to use SwitchPreference instead Checked text view , – fahd mana Oct 04 '17 at 12:22
  • in your answer you used checked text view ,,,but what i want is to use Switch Preference if don't know what is switch preference you must see it – fahd mana Oct 04 '17 at 19:49