Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

java - 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.

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

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

1 Reply

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...