home top ad

How to save state of checkBox in android

Hello friend today we will learn about how to save state of checkBox in android.We must have seen the use of checkboxes in many apps.We can use it in many ways whether it is dark mode or some more.


When we check the checkbox in the app, then when we close the app and when we open the app again, we see that the checkbox becomes unchecked.Let's see how we will save state of checkBox in our app.First of all we will create a project in Android Studio.As soon as we create the project, we will paste this code where we want to implement it. We use it in MainActivity class.

activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="130dp"
        android:text="checkBox"
        android:textColor="@color/purple_500"
        android:textSize="20sp"
        android:textStyle="bold" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Night Mode" />
</LinearLayout>
Here we have used a TextView and a Checkbox. Which we have used in MainActivity.
MainActivity.java
package com.example.darkmode;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

public class MainActivity extends AppCompatActivity {
    private CheckBox checkBox;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox);
        getSupportActionBar().setBackgroundDrawable(getDrawable(R.color.purple_700));
        checkBox = findViewById(R.id.checkBox);
         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        checkBox.setChecked(sharedPreferences.getBoolean("checkBox", false));
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                        editor.putBoolean("checkBox", true);
                        editor.apply();
                        checkBox.setChecked(true);
               
                } else {
                    editor.putBoolean("checkBox", false);
                    editor.apply();
                    checkBox.setChecked(false);
                }

            }
        });


    }
}

Here we will save state of the checkbox using SharedPreferences.Here we will save the state of the checkbox by using some parameter using SharedPreferences.Editor which will take a boolean value as parameter.And last we will editer.apply .Then checkBox.setChecked(true) will do it.After this we will start saving the state of the checkbox in our app.
thanks you.

Post a Comment

0 Comments