0

I'm new to developing with android java. I am trying to set a button's background and textcolor to colorPrimary and colorOnPrimary values from /values/themes/themes.xml so that on dark theme buttons still would look normal.

I need to get these values programmatically i think. getResources().getColor(R.color.xxx) method only returns the colors that are set from /values/colors.xml

I couldn't find any duplicates of this issue, please read carefully what i'm trying to get. Also, I didn't particularly set my own theme. I adjusted the preset themes. (themes.xml and themes.xml (night))

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Emir Halıcı
  • 21
  • 1
  • 7

1 Answers1

2

To get the value of an attribute in your theme you can use something like:

val typedValue = TypedValue();
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
val color = ContextCompat.getColor(this, typedValue.resourceId)

button.setBackgroundColor(color)

or in java:

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorSecondary, typedValue, true);
int color = ContextCompat.getColor(this, typedValue.resourceId);

button.setBackgroundColor(color);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841