Nathan
Nathan

Reputation: 165

How to change global accent colour through code

I'm making my first app in android studio. It's going well so far, but I've come across this stumbling block:

I have absolutely no idea how to edit already defined colours.

Here's some screenshots from two of the activities:

enter image description here enter image description here

Inside the layout .xml code i have coloured everything like this:

<androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/layoutReset"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/resetNutsButton"
                    android:layout_width="300dp"
                    android:layout_height="35dp"
                    android:layout_gravity="center"
                    android:layout_margin="30dp"
                    android:background="@drawable/round_corners"
                    android:backgroundTint="@color/Accent1"           //Accent colour
                    android:fontFamily="@font/lemonmilkregular"
                    android:text="Reset nuts"
                    android:textAlignment="center"
                    android:textColor="@color/Back1"                  //grey
                    android:textSize="25sp"
                    android:visibility="visible"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>

Everything in the project has been coloured using the colors.xml file and, as you can see from the 2nd picture, I would like to add the option to choose an accent colour to be used everywhere that is currently red.

I've looked through many questions and I can see that editing any of the resource files during runtime is not possible, so I was wondering what the best way is to go about this.

As I said, this is my first app, so I may just be missing something simple. Any input is appreciated, and if you need any more details please tell me.

Upvotes: 0

Views: 166

Answers (1)

Greg
Greg

Reputation: 900

It's true, you're not able to edit colours or themes at runtime. You could potentially try something like (in styles.xml):

<!-- Colors -->
<style name="Indigo">
    <item name="colorPrimary">@color/indigoColorPrimary</item>
    <item name="colorPrimaryDark">@color/indigoColorPrimaryDark</item>
    <item name="colorAccent">@color/indigoColorAccent</item>
</style>
<style name="Blue">
    <item name="colorPrimary">@color/blueColorPrimary</item>
    <item name="colorPrimaryDark">@color/blueColorPrimaryDark</item>
    <item name="colorAccent">@color/blueColorAccent</item>
</style>
<style name="Red">
    <item name="colorPrimary">@color/redColorPrimary</item>
    <item name="colorPrimaryDark">@color/redColorPrimaryDark</item>
    <item name="colorAccent">@color/redColorAccent</item>
</style>

And then in your code:

getTheme().applyStyle(R.style.Blue, true);

Also remove any places in your layouts where you're explicitly setting the colours, you just want it to use the theme's colours:

android:backgroundTint="@color/Accent1"

Upvotes: 2

Related Questions