Dark Mode Light Mode Android Example kotlin

06-Dec-2024

Dark Mode Light Mode Android Example kotlin


1/Step :  Create A New Project and Create two different theme files: one for Light Mode and another for Dark Mode.

res/values/themes.xml:


<resources>
<style name="AppTheme" parent="Theme.Material3.Dark.NoActionBar">
<!-- Customize your dark theme colors -->
<item name="colorPrimary">#040E3D</item>
<item name="colorPrimaryVariant">#B5CBDD</item>
<item name="colorOnPrimary">#FFFFFF</item>
</style>
</resources>


res/values-night/themes.xml:


<resources>
<style name="AppTheme" parent="Theme.Material3.Dark.NoActionBar">
<!-- Customize your dark theme colors -->
<item name="colorPrimary">#BB86FC</item>
<item name="colorPrimaryVariant">#3700B3</item>
<item name="colorOnPrimary">#FFFFFF</item>
</style>
</resources>




2/Step :  Add the code main_Activity.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<TextView
android:id="@+id/themeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Theme"
android:textSize="18sp"
android:padding="8dp" />

<Button
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle Theme" />
</LinearLayout>


3/Step : Add the code in ManiActivity


  • package com.alomkaisa.myapplication

    import android.content.SharedPreferences
    import android.os.Bundle
    import android.widget.Button
    import android.widget.TextView
    import androidx.activity.enableEdgeToEdge
    import androidx.appcompat.app.AppCompatActivity
    import androidx.appcompat.app.AppCompatDelegate
    import androidx.core.view.ViewCompat
    import androidx.core.view.WindowInsetsCompat

    class MainActivity : AppCompatActivity() {

    private lateinit var themeText: TextView
    private lateinit var toggleButton: Button
    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    themeText = findViewById(R.id.themeText)
    toggleButton = findViewById(R.id.toggleButton)


    sharedPreferences = getSharedPreferences("ThemePrefs", MODE_PRIVATE)
    val isDarkMode = sharedPreferences.getBoolean("isDarkMode", false)


    setThemeMode(isDarkMode)


    toggleButton.setOnClickListener {
    val newMode = !isDarkMode() // Toggle mode
    setThemeMode(newMode)


    with(sharedPreferences.edit()) {
    putBoolean("isDarkMode", newMode)
    apply()
    }
    }
    }

    private fun setThemeMode(isDarkMode: Boolean) {
    if (isDarkMode) {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    themeText.text = "Dark Mode"
    } else {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
    themeText.text = "Light Mode"
    }
    }

    private fun isDarkMode(): Boolean {
    return sharedPreferences.getBoolean("isDarkMode", false)
    }

}

Comments