Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

question from:https://stackoverflow.com/questions/65907282/how-to-change-global-accent-colour-through-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
85 views
Welcome To Ask or Share your Answers For Others

1 Answer

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...