home top ad

How to Use RadioButton to DarkMode in Android

Hello friends today we will learn about How to Use RadioButton 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 we are create a new project in android studio or paste code where want to use this function.First of all we will create a project in android studio or where we want to use this method we will paste it there.

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="10dp"
        android:text="RadioButton "
        android:textColor="@color/purple_500"
        android:textSize="20sp"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/radio_Group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Default" />


        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Night Mode" />

    </RadioGroup>

    <Button
        android:id="@+id/btn_summit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Summit" />

</LinearLayout>
MainActivity
package com.example.darkmode;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private RadioButton radioButton1, radioButton2;
    private Button btn;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = findViewById(R.id.radio_Group);
        radioButton1 = findViewById(R.id.radioButton1);
        radioButton2 = findViewById(R.id.radioButton2);
        btn = findViewById(R.id.btn_summit);
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPreferences.edit();
      final boolean isDarkMode = sharedPreferences.getBoolean("isDarkMode", false);
        radioButton1.setChecked(sharedPreferences.getBoolean("btn1", false));
        radioButton2.setChecked(sharedPreferences.getBoolean("btn2", false));
        if (isDarkMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

        } else {

            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioButton1.isChecked()) {
             
                        editor.putBoolean("btn1", true);
                        editor.apply();
                        radioButton1.setChecked(true);
                }
                else
                {
           
                    editor.putBoolean("btn1", false);
                    editor.apply();
                    radioButton1.setChecked(false);
                } if (radioButton2.isChecked()) {
         
                        editor.putBoolean("btn2", true);
                        editor.apply();
                        radioButton2.setChecked(true);
                        Toast.makeText(RadioActivity.this, "Radio button 2", Toast.LENGTH_SHORT).show();
                
                }

                else{

                    editor.putBoolean("btn2", false);
                    editor.apply();
                    radioButton2.setChecked(false);
                }
            }
        });

    }
}
Thanks

Post a Comment

0 Comments