home top ad

How to Use toggleButton to DarkMode in Android

Hello friends today we will learn about How to Use togglebutton to DarkMode in Android.Today we see in many apps, we are seeing the use of dark theme, so today we will see how we will use dark theme in our app.
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.

actvity_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>
 
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;
import androidx.appcompat.app.AppCompatDelegate;

public class ToggleActivity extends AppCompatActivity {
    private ToggleButton toggleButton;

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setBackgroundDrawable(getDrawable(R.color.purple_500));
        toggleButton = findViewById(R.id.toggleButton);
        SharedPreferences sharedPreferences = getSharedPreferences("toggle", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        final boolean isDarkMode = sharedPreferences.getBoolean("isDarkMode", false);
        toggleButton.setChecked(sharedPreferences.getBoolean("to", false));
        if (isDarkMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

        } else {

            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (toggleButton.isChecked()) {
                    if (isDarkMode) {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                        editor.putBoolean("isDarkMode", false);
                        editor.apply();
                        Toast.makeText(ToggleActivity.this, "on", Toast.LENGTH_SHORT).show();
                        editor.putBoolean("to", true);
                        editor.apply();
                        toggleButton.setChecked(true);
                    }
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    editor.putBoolean("isDarkMode", true);
                    editor.apply();
                    Toast.makeText(ToggleActivity.this, "off", Toast.LENGTH_SHORT).show();

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

    }
}
Thanks

Post a Comment

0 Comments