home top ad

How to save state of ToggleButton in android

Hello friends today we will learn about how to save state of ToggleButton in android.We use toggleButton many times in our project.But many times we use togglebutton, then we are not able to save its state. When we re-open our app, the togglebutton which is on becomes off again if we want to save the state of togglebutton then we have to use some method.
First of all we will create a new project in android studio or we will implement it in the project in which we want to use it.

activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="150dp"
        android:text="Toogle "
        android:textColor="@color/purple_500"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_gravity="center_horizontal"
        android:text="ToggleButton" />

</LinearLayout>
We have used ToogleButton in the layout in activity_main, which we will do in MainActivity.

MainActivity
package com.example.darkmode;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

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

public class MainActivity extends AppCompatActivity {
    private ToggleButton toggleButton;
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        setContentView(R.layout.activity_toogle);
        toggleButton=findViewById(R.id.toggleButton);
        SharedPreferences sharedPreferences=getSharedPreferences("toggle",MODE_PRIVATE);
        final  SharedPreferences.Editor editor=sharedPreferences.edit();
        toggleButton.setChecked(sharedPreferences.getBoolean("to",false));
        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (toggleButton.isChecked())
                {
                    Toast.makeText(ToggleActivity.this, "open...", Toast.LENGTH_SHORT).show();
                    editor.putBoolean("to",true);
                    editor.apply();
                    toggleButton.setChecked(true);
                }
                else
                {
                    Toast.makeText(ToggleActivity.this, "close...", Toast.LENGTH_SHORT).show();

                    editor.putBoolean("to",false);
                    editor.apply();
                    toggleButton.setChecked(false);
                }
            }
        });

    }
}
Thanks.

Post a Comment

0 Comments