1

I found the answer to my question here: CheckBoxPreference with additional Button? But I really did not understand how to do it. Probably due to the fact that I do not know much English. I need to put into ChekBoksPreferens button. Can anyone give an example how to do it.

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

    public class IconPreferenceScreen extends CheckBoxPreference {

        private Drawable mIcon;

        public IconPreferenceScreen(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setLayoutResource(R.layout.preference_icon);
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconPreferenceScreen, defStyle, 0);
            mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_myIcon);
        }

        @Override
        public void onBindView(View view) {
            super.onBindView(view);
            ImageView imageView = (ImageView) view.findViewById(R.id.icon);
            if (imageView != null && mIcon != null) {
                imageView.getLayoutParams().height = 150;
                imageView.getLayoutParams().width = 150;
                imageView.setImageDrawable(mIcon);
            }
        }

        public void setIcon(Drawable icon) {
            if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
                mIcon = icon;
                notifyChanged();
            }
        }

        public Drawable getIcon() {
            return mIcon;
        }

        public class CheckBoxPreferenceSubclassWithButton{
            ???? what next ????
        }
    }
Community
  • 1
  • 1
niklas1987
  • 61
  • 9

1 Answers1

0

use the below code for saving values in sharedpreferences on using checkbox validation with on button click. private SharedPreferences.Editor loginPrefsEditor; private SharedPreferences loginPreferences; //after onCreate

 CheckBox  saveCheckBox = (CheckBox) findViewById(R.id.test);
    loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);

        loginPrefsEditor = loginPreferences.edit();

// on button click


@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.sunbmit:

if (saveCheckBox.isChecked()) {


                            loginPrefsEditor.putBoolean("saveLogin", true);

                            loginPrefsEditor
                                    .putString("username", "value");

                            loginPrefsEditor
                                    .putString("password", "value");

                            loginPrefsEditor.commit();

                        } else {

                            loginPrefsEditor.clear();

                            loginPrefsEditor.commit();

                        }

}


    }
ManiTeja
  • 849
  • 1
  • 9
  • 13