Android – Implement Night Mode using Switch
Night Mode (also known as Black Mode or Dark Mode) is a screen display that uses dark background. Opposite to Light Mode which uses light background and dark text, night mode uses a dark background and light text. Dark mode reduces battery consumption, this is because the device finds it easier to run the dark background as it does not have to light the entire screen. In this article, we will build a simple app to implement night mode using Switch in android.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
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. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > <!--implementing switch --> < com.google.android.material.switchmaterial.SwitchMaterial android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id = "@+id/switchBtn" app:layout_constraintWidth_percent = "0.8" app:layout_constraintTop_toTopOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintBottom_toBottomOf = "parent" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 3: 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
package com.example.nightmodebutton; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import android.os.Bundle; import android.widget.CompoundButton; import com.google.android.material.switchmaterial.SwitchMaterial; import java.util.Objects; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initialize variable SwitchMaterial switchBtn = findViewById(R.id.switchBtn); Objects.requireNonNull(getSupportActionBar ()).setTitle( "LIGHT-NIGHT MODE SWITCH" ); // switch theme mode per user wishes // setting onCheckedListener on switch switchBtn.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked){ // checking if the switch is turned on if (isChecked) { // setting theme to night mode AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); buttonView.setText( "Night Mode" ); } // if the above condition turns false // it means switch is turned off // by-default the switch will be off else { // setting theme to light theme AppCompatDelegate.setDefaultNightMode (AppCompatDelegate.MODE_NIGHT_NO); buttonView.setText( "Light Mode" ); } } }); } } |
Output: