Get Color Value Programmatically When It's a Reference (Theme)

Get color value programmatically when it's a reference (theme)

This should do the job:

TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.theme_color, typedValue, true);
@ColorInt int color = typedValue.data;

Also make sure to apply the theme to your Activity before calling this code. Either use:

android:theme="@style/Theme.BlueTheme"

in your manifest or call (before you call setContentView(int)):

setTheme(R.style.Theme_BlueTheme)

in onCreate().

I've tested it with your values and it worked perfectly.

How to get a value of color attribute programmatically

I believe instead of this:



TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorControlNormal, typedValue, true);
int color = typedValue.data;

You should do this:



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

How to get color which is defined as attribute for different themes in Android [Kotlin]

I tested this and it works for me:

val color = MaterialColors.getColor(context, R.attr.myColor, Color.GRAY)
binding.fab.backgroundTintList = ColorStateList.valueOf(color)

This ^^^ is btw from SO, specifically from this answer and its comments: Get color value programmatically when it's a reference (theme).

how to get theme color programmatically in android java?

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);

How to get an ?attr/ value programmatically

As commented from @pskink:

    TypedValue value = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.editTextColor, value, true);
getView().setBackgroundColor(value.data);

will pull the attribute from the theme currently assigned to the context.

Thanks @pskink!

Programmatically set a view's color to ?android:attr color attributes

You can use android.R.attr.textColorPrimary.

Something like:

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

In Java:

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

Access Android reference style color attribute programmatically

Does something like this help?

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.textColorHint, typedValue, true);
button.setBackgroundColor(typedValue.data);


Related Topics



Leave a reply



Submit