0

I´m building an appp that display news from an API but It also have a settings fragment from where I want to have the ability to change the theme of the whole app with an X amount of buttons (So far only one until I manage to make it work) once I press the button I want the color of the whole app to change but I can´t figure it out a way to make this work. I found this solution but I can understand how I can implement it myself. I´m working in Kotlin.

Here is the code of the fragmentSettings:

class FragmentSettings : Fragment() {

    private lateinit var binding: FragmentSettingsBinding


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentSettingsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        changeToGreen()
    }

    private fun changeToGreen() {
        binding.bRed.setOnClickListener {
        }
    }
}

The theme.xml file: I create another style as said in the other answer but I´m stuck there. No idea on how to use it.

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.NewsScrap" parent="Theme.MaterialComponents.DayNight">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/onprimary</item>
        <item name="colorPrimaryVariant">@color/onprimary</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/secondary</item>
        <item name="colorSecondaryVariant">@color/secondaryvariant</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="ThemeNewsScrapGreen">
        <item name="colorPrimary">@color/md_green_500</item>
        <item name="colorPrimaryDark">@color/md_green_700</item>
        <item name="colorOnPrimary">@color/md_green_700</item>
    </style>


    <style name="imageCircular">
        <item name="cornerSize">10%</item>
    </style>
</resources>

I hope there is a way to make this work because I´m desperate. Thanks a lot !!

Ince I press the button I want to the color of the whole app to change but I can´t figure it out a way to make this work. I found this solution but I can understand how I can implement it myself. I´m working in Kotlin.

  • I will answer my own question, I just discover the perfect library for this and everything that has something to do with theme changes. Very simple to use and works with everything !! Hope it helps :) https://github.com/imandolatkia/Android-Animated-Theme-Manager/issues/8 PD: I haven´t created but this guys are incredible – Fran Segura Cruz Dec 06 '22 at 17:00

0 Answers0