Change background color of fragments dynamically from a switch

Viewed 41

I am trying to have all fragments change to the background color of red using a SwitchPreferenceCompat but am having trouble writing the listener code and MainActivity code. I have tried some things but am not too familiar with how to write the listener code to be able to write the mainactivity code to use it properly. I am not really looking for a solution but rather being pointed in the right direction.

Thank you

Here's the relevant code: Preferences xml

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory android:title="Game Settings">
        <SeekBarPreference
            android:defaultValue="10"
            android:key="pennyCount"
            android:max="20"
            android:summary="@string/starting_penny_count_new_game"
            android:title="@string/starting_penny_count"
            app:min="1"
            app:showSeekBarValue="true" />

        <SwitchPreferenceCompat
            android:key="fastAI"
            android:summary="@string/fast_ai_summary"
            android:title="@string/fast_ai" />
    </PreferenceCategory>

    <PreferenceCategory android:title="Theme Settings">
        <DropDownPreference
            android:defaultValue="AppTheme"
            android:entries="@array/themes"
            android:entryValues="@array/theme_values"
            android:key="theme"
            android:title="@string/app_theme"
            app:useSimpleSummaryProvider="true" />

        <ListPreference
            android:defaultValue="System"
            android:entries="@array/theme_modes"
            android:entryValues="@array/theme_mode_values"
            android:key="themeMode"
            android:title="@string/theme_mode"
            app:useSimpleSummaryProvider="true" />
    </PreferenceCategory>

    <PreferenceCategory android:title="About the App">
        <Preference
            android:key="credits"
            android:summary="@string/about_app_summary"
            android:title="@string/about_penny_drop" />
    </PreferenceCategory>
    <SwitchPreferenceCompat
        android:defaultValue="false"
        android:key="switchRed"
        android:title="Turn Background Color Red"
        app:theme="@style/SwitchTheme"/>
</PreferenceScreen>

MainActivity.kt

package dev.mfazio.pennydrop

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.style.BackgroundColorSpan
import android.view.Menu
import android.view.MenuItem
import android.widget.Switch
import androidx.appcompat.app.AppCompatDelegate
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.onNavDestinationSelected
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.preference.PreferenceManager
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        val prefs =
            PreferenceManager.getDefaultSharedPreferences(this)

        val themeId = when (prefs.getString("theme", "AppTheme")) {
            "Crew" -> R.style.Crew
            "FTD" -> R.style.FTD
            "GPG" -> R.style.GPG
            "Hazel" -> R.style.Hazel
            "Kotlin" -> R.style.Kotlin
            else -> R.style.AppTheme
        }

        setTheme(themeId)


        val nightMode = when (prefs.getString("themeMode", "")) {
            "Light" -> AppCompatDelegate.MODE_NIGHT_NO
            "Dark" -> AppCompatDelegate.MODE_NIGHT_YES
            else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
        }

        AppCompatDelegate.setDefaultNightMode(nightMode)

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.containerFragment) as NavHostFragment
        this.navController = navHostFragment.navController

        val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav)
        bottomNav.setupWithNavController(this.navController)

        val appBarConfiguration = AppBarConfiguration(bottomNav.menu)
        setupActionBarWithNavController(this.navController, appBarConfiguration)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)

        menuInflater.inflate(R.menu.options, menu)

        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean =
        if (this::navController.isInitialized) {
            item.onNavDestinationSelected(this.navController) ||
                super.onOptionsItemSelected(item)
        } else false

    override fun onSupportNavigateUp(): Boolean =
        (this::navController.isInitialized && this.navController.navigateUp()) ||
            super.onSupportNavigateUp()
}

SettingFragment

package dev.mfazio.pennydrop.fragments

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import androidx.navigation.fragment.findNavController
import androidx.preference.*
import dev.mfazio.pennydrop.R

class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(
        savedInstanceState: Bundle?,
        rootKey: String?
    ) {
        setPreferencesFromResource(R.xml.preferences, rootKey)

        val themePreference = findPreference<DropDownPreference?>("theme")
        val themeModePreference = findPreference<ListPreference?>("themeMode")
        val creditsPreference = findPreference<Preference?>("credits")
        val switchPreference = findPreference<SwitchPreferenceCompat>("switchRed")


        themePreference?.onPreferenceChangeListener =
            Preference.OnPreferenceChangeListener { _, _ ->

                activity?.recreate()

                true
            }

        themeModePreference?.setDefaultValue(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

        themeModePreference?.onPreferenceChangeListener =
            Preference.OnPreferenceChangeListener { _, newValue ->
                val nightMode = when (newValue?.toString()) {
                    "Light" -> AppCompatDelegate.MODE_NIGHT_NO
                    "Dark" -> AppCompatDelegate.MODE_NIGHT_YES
                    else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
                }

                AppCompatDelegate.setDefaultNightMode(nightMode)
                true
            }

        creditsPreference?.onPreferenceClickListener =
            Preference.OnPreferenceClickListener { _ ->
                this.findNavController().navigate(R.id.aboutFragment)
                true
            }
    }
}

Styles xml

<resources>
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorSurface">@color/nightLightGray</item>
    </style>

    <style name="Kotlin" parent="AppTheme">
        <item name="colorPrimary">@color/kotlinBlue</item>
        <item name="colorPrimaryDark">@color/kotlinPurple</item>
        <item name="colorAccent">@color/kotlinOrange</item>
    </style>

    <style name="Crew" parent="AppTheme">
        <item name="colorPrimary">@color/crewBlue</item>
        <item name="colorPrimaryDark">@color/crewLightBlue</item>
        <item name="colorAccent">@color/crewGold</item>
        <item name="colorSurface">@color/crewBlue</item>
    </style>

    <style name="FTD" parent="AppTheme">
        <item name="colorPrimary">@color/ftdGreen</item>
        <item name="colorPrimaryDark">@color/ftdGreen</item>
        <item name="colorAccent">@color/ftdBlue</item>
    </style>

    <style name="GPG" parent="AppTheme">
        <item name="colorPrimary">@color/packGreen</item>
        <item name="colorPrimaryDark">@color/packGreen</item>
        <item name="colorAccent">@color/packGold</item>
        <item name="colorSurface">@color/packGreenDark</item>
    </style>

    <style name="Hazel" parent="AppTheme">
        <item name="colorPrimary">@color/hazelBlue</item>
        <item name="colorPrimaryDark">@color/hazelDarkBlue</item>
        <item name="colorAccent">@color/hazelPink</item>
    </style>

    <style name="SwitchTheme" parent="AppTheme">
        <item name="android:colorBackground">#FF0000</item>
    </style>

</resources>
0 Answers
Related