0

I want to change a button that has a shape with a color set:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/custom"/>
    <corners android:bottomLeftRadius="10dp"/>
</shape>

and in my colors list resources I have <color name="custom">#A281E0</color>. Is it possible to programmatically change the color of custom? Thanks in advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ioan S.
  • 154
  • 3
  • 14

3 Answers3

1

Maybe it will be better to use dynamically color changing, like this:

String color = "your hex color"
Int colorToUse = Color.parseColor(color)

Then set a background color of your button:

Int buttonId = findViewById(R.id.button_id)
buttonId.setBackgroundColor(colorToUse)

Set a color that you need to color variable. Something like this.

Max Shwed
  • 232
  • 2
  • 10
  • Then you can handle it what you need, like `onClick()` or in `onCreate()` methods etc. – Max Shwed Jan 31 '20 at 22:37
  • I agree with this option, but my intention was to automatically change color for dozens of buttons by changing one color they all target – Ioan S. Jan 31 '20 at 23:08
0

I can't comment because i don't have enough reputation.

Why do you need to do this? Perhaps there is another plan of attack to solve the requirement, for instance a state list drawable, which will allow for different drawables based on varying state - ie selected, focused, etc.

https://developer.android.com/guide/topics/resources/drawable-resource#StateList

Chris
  • 4,662
  • 2
  • 19
  • 27
-1

Because I'm a newbie I can't mark this a duplicate but I think this question will give you the answer you want. Apparently, you can create a class that extends Resources and override the method getColor(int).

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61