Home Tutorials Day Night theme for Night Mode in Android

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!!!!!

36 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.

  13. Good day! I could have sworn I’ve been to this blog before but after checking through some of the post I realized it’s new to me.

    Anyhow, I’m definitely glad I found it and I’ll be bookmarking
    and checking back often!

  14. Тһis іs the right webpage for anyboԀy who hopes to find out about this
    topic. You know sօ much its almost hard to argue wіth yоu (not thɑt I reаlly will need to…HaHa).
    Υou certainlү put a new spin օn а topic that has ƅeen discusseԁ for years.
    Wonderful stuff, just wonderful!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

2 Best ways :How to replace a lost Galaxy Buds earbud

Well, Galaxy buds are truly one of the cheapest truly wireless buds you can purchase apart from Apple's Airpods. But what if you already have...

How to import your pictures from Facebook into Google Photos in 10 minutes

We know that Google provides one of the best cloud storage for our photos for free(not for original resolution.. unless you have Pixel 3...

Apple iOS 12.2 updates are rolling out

Apple iOS 12.2 software update is now rolling out to iPhones and iPads. In this update, Apple brings Apple News Plus, it is apple...

How to enable Developer Options on android device

Android is an open source operating the mobile system and provides a ton of features on board without any modification to be done for...