Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I set color programmatically in Kotlin?

TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

How do I change the background color in programmatically?

xml which is under the values folder,then you should call the following: root. setBackgroundColor(getResources(). getColor(R.color.name));


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.


To add to the accepted answer, if you're using kotlin.

@ColorInt
fun Context.getColorFromAttr(
    @AttrRes attrColor: Int,
    typedValue: TypedValue = TypedValue(),
    resolveRefs: Boolean = true
): Int {
    theme.resolveAttribute(attrColor, typedValue, resolveRefs)
    return typedValue.data
}

and then in your activity you can do

textView.setTextColor(getColorFromAttr(R.attr.color))


We can use the utility class provided by Material Design library:

int color = MaterialColors.getColor(context, R.attr.theme_color, Color.BLACK)

NOTE: Color.BLACK is the default color in case the attribute supplied to the u


This worked for me:

int[] attrs = {R.attr.my_attribute};
TypedArray ta = context.obtainStyledAttributes(attrs);
int color = ta.getResourceId(0, android.R.color.black);
ta.recycle();

if you want to get the hexstring out of it:

Integer.toHexString(color)

2021/January/8

If you want to get color from theme attributes, use the following steps.

Create a variable my_color and store the color from theme attributes as,

val my_color = MaterialColors.getColor(<VIEWOBJECT>, R.attr.<YOUATRRIBUTENAME>)

In place of <VIEWOBJECT>, pass a view object where you want to use the color, (behind the scenes it's just used to get the context as <VIEWOBJECT>.getContext() so that it can access resource i.e theme attribute values) . In place of <YOURATTRIBUTENAME>, use the name of the atrribute that you want to access

Example 1:

If you want want to get color referenced by theme attributes from certain activity. Create a variable that represents a view object on which you want to use the color. Here i have a textView in my activity, i'll just reference its object inside textview variable and pass it to the getColor method and behind the scenes it'll use that object to just get the context, so that it can access theme attribute values

val textview: TextView = findViewById(R.id.mytextview)
val my_color = MaterialColors.getColor(textView, R.attr<YOURATTRIBUTENAME>)

Example 2 :

If you want to get color from theme attributes inside a custom view then just use,

val my_color = MaterialColors.getColor(this, R.attr.<YOUATRRIBUTENAME>)

Inside a custom view this refers to the object of the custom view, which is in fact a view object.


Add this to your build.gradle (app):

implementation 'androidx.core:core-ktx:1.1.0'

And add this extension function somewhere in your code:

@ColorInt
@SuppressLint("Recycle")
fun Context.themeColor(
    @AttrRes themeAttrId: Int
): Int {
    return obtainStyledAttributes(
        intArrayOf(themeAttrId)
    ).use {
        it.getColor(0, Color.MAGENTA)
    }
}

If you want to get multiple colors you can use:

int[] attrs = {R.attr.customAttr, android.R.attr.textColorSecondary, 
        android.R.attr.textColorPrimaryInverse};
Resources.Theme theme = context.getTheme();
TypedArray ta = theme.obtainStyledAttributes(attrs);

int[] colors = new int[attrs.length];
for (int i = 0; i < attrs.length; i++) {
    colors[i] = ta.getColor(i, 0);
}

ta.recycle();