Day Night theme for Night Mode in Android

In this tutorial, we will discuss and learn, how to use DayNight theme of an Android in your Android Application to enable.

A new theme has been added by Android to AppCompat Theme.AppCompat.DayNight with support library 23.2.0. This enables the user to toggle switch between night mode and day mode. This feature can be very useful if you have a reading application in your android app, let start with implementing night mode in android.

AppCompatDelegate

AppCompatDelegate is a class which represents a delegate which you can use to extend AppCompat’s support to any Activity.

It takes one of the following methods:-

  • MODE_NIGHT_YES – It enables night/dark theme.
  • MODE_NIGHT_NO – It enables the day/light theme.
  • MODE_NIGHT_FOLLOW_SYSTEM – It is the default option. Uses the system’s settings to determine the time of day and toggles NightMode accordingly.
  • MODE_NIGHT_AUTO – Changes between day/night based on the time of day.

Must Read: Google Login And Registration For Android Using Firebase Authentication

Follow the below steps to add this functionality in your Application:-

  1. ADD THEME Open styles.xml by app>res>values>styles.xmlAdd parent=“Theme.AppCompat.DayNight.DarkActionBar”

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

2. Add Activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Switch
        android:id="@+id/switch_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="80dp"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DARKMODE TUTORIAL"
        android:layout_gravity="center"
        android:textSize="30dp"
        android:layout_marginTop="50dp"/>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BUTTON"
        android:layout_marginTop="50dp"/>

</LinearLayout>

3. Add MainActivity.java

package com.example.darkmode;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.mahfa.dnswitch.DayNightSwitch;
import com.mahfa.dnswitch.DayNightSwitchListener;

public class MainActivity extends AppCompatActivity {

    Switch aSwitch;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aSwitch=findViewById(R.id.switch_toggle);
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               if(isChecked)
               {
                   //if day mode is enabled, set night mode using AppCompatDelegate class.
                   Toast.makeText(MainActivity.this, "Night selected", Toast.LENGTH_SHORT).show();
                   getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
           }
           else {
                   //if night mode is enabled, set day mode using AppCompatDelegate class.
                   Toast.makeText(MainActivity.this, "Day mode selected", Toast.LENGTH_SHORT).show();
                   getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);

                }
            }
        });
    }
}

Now, you are ready to run your application.

When you will run, you can see your application like this:-

You can also access code through our following GitHub link:-

GITHUB

Hope you like this Article. Stay tuned for more such android related Articles.

For any Query, Comment down Below!!!!!

Recent Articles

8 Best Offline Football Games for Android 2020

Are you looking for the best offline football games? Football is one of the world's most popular games. Even it is one...

Samsung Galaxy S20+ leaked, image surface online

Just a few days beofre Samsung is going to showcase the new S20 series, Galaxy S20+ leaked images online somehow showing the...

Samsung Galaxy Note 10 lite: 2 Year old processor smartphone, Best Mid ranger?

Earlier this week Samsung unveiled Samsung Note 10 lite and Samsung Galaxy S10 lite. It was very surprising as Galaxy lineup has...

Easy Trick: How to Merge Finder windows in Mac

If you have used old MAC OS then you might remember you can't merge the windows and it was very difficult to work...

[Updated] Temporarily Free Games and Apps on Google Play Store

Every week there are free games and apps on the Google Play store. Here is the list of free games and apps on the...

29 COMMENTS

  1. Magnificent beat ! I would like to apprentice even as you amend your website, how could
    i subscribe for a blog web site? The account aided me a applicable deal.

    I have been a little bit familiar of this your broadcast offered
    brilliant transparent concept

  2. Howdy very cool web site!! Guy .. Beautiful .. Amazing ..
    I’ll bookmark your web site and take the feeds also?I’m satisfied to
    find a lot of useful info right here in the put up, we need work out more
    strategies on this regard, thanks for sharing.

  3. Hi! Someone in my Myspace group shared this website with us so
    I came to give it a look. I’m definitely
    loving the information. I’m bookmarking and will be tweeting
    this to my followers! Great blog and outstanding style and design.

  4. Thanks so much for another post. I be able to get that kind of information information. friend, and exactly.

  5. Thanks for bookmarking AndroidHire. You would sure like to read our latest content which comes regularly

  6. Thanks a lot patricia for your valuable feedback! Do subscribe us for more awesome content in Android genre

  7. Hi there, I discovered your website by means of Google
    while looking for a comparable subject, your site
    got here up, it seems to be good. I’ve bookmarked it in my google bookmarks.
    Cheers!

  8. Great work! That is the type of information that are supposed to be
    shared across the internet. Disgrace on the seek engines for now not positioning this publish
    higher! Come on over and discuss with my website . Thank you
    =)

  9. Incredible! This blog looks exactly like my old one!
    It’s on a entirely different subject but it has pretty much the same page layout and design.
    Outstanding choice of colors!

  10. Regards for this post, I am a big fan of this internet site would like to go along updated.

  11. Awesome site! I am loving it!! Will be back later to read some more.
    I am bookmarking your feeds also

  12. Hello.This article was really motivating, especially since I was
    investigating for thoughts on this matter last Wednesday.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay on Top - Get the daily news in your inbox