How to Switch Themes in Android Using RadioButtons?

22 Mar, 2021
We have seen android app comes with two different types of mode or theme which are dark mode or light mode or we also called them as night and morning mode. In this article, we will implement the light and dark mode in android using RadioButton. 
We will be building a simple application in which we will be displaying simple text and RadioButton in which we will be switching our theme from light to dark using simple RadioButtons. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">      <!--text view for displaying simple heading-->    <TextView        android:id="@+id/idTVHeading"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="40dp"        android:gravity="center_horizontal"        android:text="Welcome to Geeks for Geeks"        android:textAlignment="center"        android:textSize="20sp" />      <!--text view for displaying the selected theme-->    <TextView        android:id="@+id/idtvTheme"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/idTVHeading"        android:layout_marginTop="30dp"        android:gravity="center_horizontal"        android:text="System Default Theme"        android:textAlignment="center"        android:textSize="20sp" />      <!--radio group for switching theme-->    <RadioGroup        android:id="@+id/idRGgroup"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/idtvTheme"        android:layout_marginTop="30dp"        android:orientation="vertical"        android:padding="4dp">          <!--radio button for light theme-->        <RadioButton            android:id="@+id/idRBLight"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:layout_margin="5dp"            android:padding="5dp"            android:text="Light" />          <!--radio button for dark theme-->        <RadioButton            android:id="@+id/idRBDark"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:layout_margin="5dp"            android:layout_weight="1"            android:padding="5dp"            android:text="Dark" />      </RadioGroup>      </RelativeLayout>
Step 3: Working with the colors.xml file 
Navigate to the app > res > values > colors.xml and add the below code to it.
XML
<?xml version="1.0" encoding="utf-8"?><resources>    <color name="purple_200">#0F9D58</color>    <color name="purple_500">#0F9D58</color>    <color name="purple_700">#0F9D58</color>    <color name="teal_200">#0F9D58</color>    <color name="teal_700">#FF018786</color>    <color name="black">#FF000000</color>    <color name="white">#FFFFFFFF</color></resources>
Step 4: Working with themes.xml for light mode
Navigate to the app > res > values > themes > themes.xml for light mode and add the below code to it. 
XML
<resources xmlns:tools="http://schemas.android.com/tools">    <!-- Base application theme. -->    <style name="Theme.Your Package Name" parent="Theme.MaterialComponents.DayNight.DarkActionBar">        <!-- Primary brand color. -->        <item name="colorPrimary">@color/purple_500</item>        <item name="colorPrimaryVariant">@color/purple_700</item>        <item name="colorOnPrimary">@color/white</item>        <!-- Secondary brand color. -->        <item name="colorSecondary">@color/teal_200</item>        <item name="colorSecondaryVariant">@color/teal_700</item>        <item name="colorOnSecondary">@color/black</item>        <!--below is the text color-->        <item name="android:textColor">@color/black</item>        <!-- Status bar color. -->        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>        <!-- Customize your theme here. -->    </style></resources>
Step 5: Working with themes.xml(night) for dark mode.
Navigate to the app > res > values > themes > themes.xml(night) for dark mode and add the below code to it. 
XML
<resources xmlns:tools="http://schemas.android.com/tools">    <!-- Base application theme. -->    <style name="Theme.Your Package Name" parent="Theme.MaterialComponents.DayNight.DarkActionBar">        <!-- Primary brand color. -->        <item name="colorPrimary">@color/purple_200</item>        <item name="colorPrimaryVariant">@color/purple_700</item>        <item name="colorOnPrimary">@color/white</item>        <!-- Secondary brand color. -->        <item name="colorSecondary">@color/teal_200</item>        <item name="colorSecondaryVariant">@color/teal_200</item>        <item name="colorOnSecondary">@color/white</item>        <!--text color-->        <item name="android:textColor">@color/white</item>        <!-- Status bar color. -->        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>        <!-- Customize your theme here. -->    </style></resources>
Step 6: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;import android.widget.RadioGroup;import android.widget.TextView;  import androidx.appcompat.app.AppCompatActivity;import androidx.appcompat.app.AppCompatDelegate;  public class MainActivity extends AppCompatActivity {    // initializing variables for    // our radio group and text view.    private RadioGroup radioGroup;    private TextView themeTV;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                  // initializing all our variables.        radioGroup = findViewById(R.id.idRGgroup);        themeTV = findViewById(R.id.idtvTheme);                  // on below line we are setting on check change method for our radio group.        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                // on radio button check change                switch (checkedId) {                    case R.id.idRBLight:                        // on below line we are checking the radio button with id.                        // on below line we are setting the text to text view as light mode.                        themeTV.setText("Light Theme");                        // on below line we are changing the theme to light mode.                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);                        break;                    case R.id.idRBDark:                        // this method is called when dark radio button is selected                        // on below line we are setting dark theme text to our text view.                        themeTV.setText("Dark Theme");                        // on below line we are changing the theme to dark mode.                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);                        break;                }            }        });    }}
Now run your app and see the output of the app. 
Output:
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.