TeoSH
TeoSH

Reputation: 13

How to uncheck and save state of checkbox with a button press

I am trying to make a checkbox that will save the state that it's in when you click it so that when you have fully closed the app, it will be in the same state when you open the app again. I have managed to do this. I also have a button that will change the state of the checkbox to unchecked when you click it. but It doesn't save the state of the checkbox when you use the button.

Java:

public class MainActivity extends AppCompatActivity {


EditText textView;
Button button;
Vibrator vibrator;
CheckBox checkBox;


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



        textView = (EditText) findViewById(R.id.textView);
    button = (Button) findViewById(R.id.button);
    CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);

    //Knapp//
    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    String oldItem = sharedPref.getString("oldItem", "Ingenting er lagret enda...");

    //Checkbox//
    SharedPreferences settings = getSharedPreferences("mysettings", 0);
    SharedPreferences.Editor editor = settings.edit();
    checkBox.setChecked(settings.getBoolean("checkBox", false));


    textView.setText(oldItem);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("oldItem", textView.getText().toString());
            editor.apply();

            final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
            checkBox.setChecked(false);

            button = findViewById(R.id.button);
            vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);


            vibrator.vibrate(30);
            Animation animation = new AlphaAnimation(1.0f, 0.5f);
            animation.setDuration(100);
            button.startAnimation(animation);

           boolean checkBoxValue = checkBox.isChecked();
            editor.putBoolean("checkBox", checkBoxValue);
            editor.apply();

        }

    });

    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            vibrator.vibrate(20);

            boolean checkBoxValue = checkBox.isChecked();
            editor.putBoolean("checkBox", checkBoxValue);
            editor.apply();


        }
    });


};

XML:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2A3039"
android:paddingHorizontal="60dp"
tools:context=".MainActivity"
android:clipToPadding="false">


<EditText
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/textbox"
    android:fontFamily="sans-serif"
    android:gravity="center_horizontal|center_vertical"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:text="Hello World!"
    android:textColor="#FFFFFF"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.495"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.123" />

<Button
    android:id="@+id/button"
    android:layout_width="150dp"
    android:layout_height="70dp"
    android:layout_marginTop="224dp"
    android:background="@drawable/blue_button"
    android:fontFamily="sans-serif-black"
    android:gravity="center_horizontal|center_vertical"
    android:onClick="tapToAnimate"
    android:text="Bytt..."
    android:textColor="@color/white"
    android:textSize="20sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.34" />


<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="#01B075"
    android:scaleX="2"
    android:scaleY="2"
    android:shadowColor="#cf1d1d"
    android:shadowDx="0.0"
    android:shadowDy="0.0"
    android:shadowRadius="8"
    android:text="@null"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.072" />


    </androidx.constraintlayout.widget.ConstraintLayout>''

Any help would be appreciated

Upvotes: 1

Views: 71

Answers (1)

Rus_o
Rus_o

Reputation: 131

In your button click listener you're creating SharedPreferences editor from :

//Knapp//
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
....

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("oldItem", textView.getText().toString());
        editor.apply();

       ....
       boolean checkBoxValue = checkBox.isChecked();
        editor.putBoolean("checkBox", checkBoxValue);
        editor.apply();

    }

});

while in checkbox listener you were using editor that was created from

//Checkbox//
SharedPreferences settings = getSharedPreferences("mysettings", 0);

which made the result if your checkbox will be checked or not on creation:

      checkBox.setChecked(settings.getBoolean("checkBox", false));

try to delete the first line in you button onClick so it will use the same editor, or if you need both of them, then make sure to update the other editor that was created from settings as well.


on another note - you created the elements in your class:

EditText textView;
Button button;
Vibrator vibrator;
CheckBox checkBox;

and found them by id in your onCreate, don't to re-find them in each listener, just use the element you already declarer earlier

Upvotes: 1

Related Questions