This video shows a very simple code to control the display of your phone and toggle between night mode (also called dark mode) ON and OFF from your Android App.
It uses UiModeManager to do the same and simply sets the Night Mode to Yes and NO for ON and OFF respectively.
For any suggestions, questions or appreciations, please reach out to us at: or email us at programmerworld1990@gmail.com
Java Source Code:
package com.example.mynightmodeapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.UiModeManager;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private UiModeManager uiModeManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
}
public void NightModeON(View view){
uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
}
public void NightModeOFF(View view){
uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
}
}
Layout XML File:
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="124dp"
android:onClick="NightModeON"
android:text="@string/night_mode_on"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="78dp"
android:onClick="NightModeOFF"
android:text="@string/night_mode_off"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mynightmodeapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Strings.xml resource file
<resources>
<string name="app_name">My Night Mode Application</string>
<string name="night_mode_off">NIGHT MODE OFF</string>
<string name="night_mode_on">NIGHT MODE ON</string>
</resources>