Setting Up the AppCompat DayNight Theme
First, ensure you have the necessary dependencies in yourbuild.gradle
file.
Step 1: Add Dependencies
Open yourbuild.gradle
file and add the AndroidX AppCompat dependency:
1 2 3 4 |
dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.core:core-ktx:1.6.0' } |
Step 2: Define the Theme in styles.xml
Open yourres/values/styles.xml
file and define the AppCompat DayNight theme:
1 2 3 4 5 6 7 8 9 |
<resources> <!-- Base application theme --> <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> <!-- Customize your theme here --> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryDark">@color/purple_700</item> <item name="colorAccent">@color/teal_200</item> </style> </resources> |
Example: Creating a Simple UI with Toggle for DayNight Theme
Let’s create a simple UI that includes a toggle button to switch between day and night modes manually.Step 3: Define the Layout
Create an XML layout file (activity_main.xml
) with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" android:gravity="center"> <TextView android:id="@+id/greetingTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" android:textSize="24sp" android:layout_marginBottom="24dp"/> <Button android:id="@+id/toggleThemeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Toggle Theme"/> </LinearLayout> |
Step 4: Initialize in Activity
In yourMainActivity.java
or MainActivity.kt
file, initialize the views and add functionality to toggle the theme.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.example.daynighttheme; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView greetingTextView = findViewById(R.id.greetingTextView); Button toggleThemeButton = findViewById(R.id.toggleThemeButton); // Load the saved theme preference SharedPreferences sharedPreferences = getSharedPreferences("ThemePref", MODE_PRIVATE); boolean isNightMode = sharedPreferences.getBoolean("NightMode", false); if (isNightMode) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); } toggleThemeButton.setOnClickListener(v -> { boolean nightMode = (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES); if (nightMode) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); saveThemePreference(false); } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); saveThemePreference(true); } }); } private void saveThemePreference(boolean isNightMode) { SharedPreferences.Editor editor = getSharedPreferences("ThemePref", MODE_PRIVATE).edit(); editor.putBoolean("NightMode", isNightMode); editor.apply(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.example.daynighttheme import android.content.SharedPreferences import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val greetingTextView: TextView = findViewById(R.id.greetingTextView) val toggleThemeButton: Button = findViewById(R.id.toggleThemeButton) // Load the saved theme preference val sharedPreferences: SharedPreferences = getSharedPreferences("ThemePref", MODE_PRIVATE) val isNightMode = sharedPreferences.getBoolean("NightMode", false) if (isNightMode) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) } toggleThemeButton.setOnClickListener { val nightMode = AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES if (nightMode) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) saveThemePreference(false) } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) saveThemePreference(true) } } } private fun saveThemePreference(isNightMode: Boolean) { val editor: SharedPreferences.Editor = getSharedPreferences("ThemePref", MODE_PRIVATE).edit() editor.putBoolean("NightMode", isNightMode) editor.apply() } } |
Need Debugging? – Try RobotQA and Start Debugging on Real Devices. Download Plugin