Commit b2bc3455 authored by Timothé KOBAK's avatar Timothé KOBAK

Dark mode implemented

parent d1ee753c
......@@ -40,6 +40,7 @@ android {
dependencies {
implementation ("androidx.preference:preference-ktx:1.1.1")
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
......@@ -48,6 +49,7 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
implementation("androidx.navigation:navigation-fragment-ktx:2.7.7")
implementation("androidx.navigation:navigation-ui-ktx:2.7.7")
implementation("androidx.preference:preference:1.2.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
......
......@@ -13,10 +13,13 @@
android:theme="@style/Theme.ElBuenoPeso"
tools:targetApi="31">
<activity
android:name=".AboutActivity"
android:name="settingActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:name="AboutActivity"
android:exported="false" />
<activity
android:name="MainActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
......
......@@ -11,6 +11,10 @@ import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.elbuenopeso.databinding.ActivityMainBinding
import androidx.appcompat.widget.SwitchCompat
import androidx.preference.SwitchPreferenceCompat;
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
......@@ -53,10 +57,12 @@ class MainActivity : AppCompatActivity() {
}
R.id.settings -> {
Toast.makeText(this, "Settings Selected", Toast.LENGTH_SHORT).show()
startActivity(Intent(this, settingActivity::class.java))
return true
}
R.id.exit -> {
Toast.makeText(this, "Exit Selected", Toast.LENGTH_SHORT).show()
finish()
return true
}
else -> return super.onOptionsItemSelected(item)
......
package com.example.elbuenopeso
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.widget.SwitchCompat
import androidx.preference.PreferenceManager
import androidx.core.content.ContextCompat
import com.example.elbuenopeso.R
class settingActivity : AppCompatActivity() {
private lateinit var themeSwitch: SwitchCompat
private lateinit var titleTextView: TextView
private lateinit var descriptionTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_setting)
themeSwitch = findViewById(R.id.themeSwitch)
titleTextView = findViewById(R.id.titleTextView)
descriptionTextView = findViewById(R.id.descriptionTextView)
// Retrieve the current theme setting
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val isNightMode = sharedPreferences.getBoolean("night_mode", false)
themeSwitch.isChecked = isNightMode
// Set up click listener for theme switch
themeSwitch.setOnCheckedChangeListener { _, isChecked ->
applyTheme(isChecked)
}
// Apply initial theme
applyTheme(isNightMode)
// Listen for changes in night mode
delegate.localNightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
private fun applyTheme(isNightMode: Boolean) {
// Update the theme setting
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
sharedPreferences.edit().putBoolean("night_mode", isNightMode).apply()
// Apply the selected theme
if (isNightMode) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
titleTextView.setTextColor(ContextCompat.getColor(this, R.color.text_color_dark))
descriptionTextView.setTextColor(ContextCompat.getColor(this, R.color.text_color_dark))
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
titleTextView.setTextColor(ContextCompat.getColor(this, R.color.text_color))
descriptionTextView.setTextColor(ContextCompat.getColor(this, R.color.text_color))
}
}
}
<?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"
android:background="@android:color/white"
tools:context=".MainActivity">
<!-- Title -->
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Theme Settings"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="@color/text_color"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
<!-- Description -->
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose your preferred theme"
android:textSize="16sp"
android:textColor="@color/text_color"
app:layout_constraintTop_toBottomOf="@id/titleTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
<!-- Switch -->
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/themeSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Dark Mode"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@id/descriptionTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
......@@ -10,5 +10,7 @@
<color name="bluee">#284B63</color>
<color name="blueee">#284B63</color>
<color name="navy">#3C6E71</color>
<color name="text_color">#000000</color> <!-- Default text color -->
<color name="text_color_dark">#FFFFFF</color> <!-- Text color for dark mode -->
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme.Light" parent="Theme.AppCompat.Light">
<!-- Customize light theme attributes here -->
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<!-- Customize dark theme attributes here -->
</style>
</resources>
\ No newline at end of file
......@@ -2,4 +2,5 @@
plugins {
id("com.android.application") version "8.2.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment