20

When I use resolveAttribute() to find out a color value of ?attr/colorControlNormal, I got 236:

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

But when I use an XML layout with the following TextView element:

<TextView
  android:id="@+id/textView"
  style="?android:attr/textAppearance"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textColor="?attr/colorControlNormal"
  android:text="@null" />

...and the following Java code:

View textView = findViewById(R.id.textView);
int color = ((TextView) textView).getCurrentTextColor();
// -1979711488

I got a color value of -1979711488


Why those results vary? I expected to get same color values, but they are not.

The second approach (I believe) returns a correct color value. Why is my first approach wrong?

I would prefer to obtain the color value of ?attr/colorControlNormal without a need of using actual element. How can I do that?

7
  • what do you see on the logcat when you Log.d the value of typedValue.coerceToString()? Commented Mar 19, 2018 at 11:24
  • @pskink res/color/secondary_text_material_light.xml Commented Mar 19, 2018 at 11:50
  • and what about simple typedValue.toString()? Commented Mar 19, 2018 at 11:52
  • @pskink TypedValue{t=0x3/d=0xec "res/color/secondary_text_material_light.xml" a=1 r=0x10601e8} Commented Mar 19, 2018 at 11:56
  • 1
    see mbcdev.com/2017/01/16/… Commented Mar 19, 2018 at 12:07

4 Answers 4

55

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Is there some similar way to get a font size for ?android:attr/textAppearance style? or a font color for ?android:attr/buttonBarNeutralButtonStyle style? Please advise. Thank you.
Use obtainStyledAttributes passing down android.R.attr.textAppearance. See an example here.
Styles contain many attributes, I assume. How can I use that with ta.getResourceId? What index should I use to get color and what index to get font-size? I am confused. Please clarify. Thanks
I assume this way: if you have specified, that you want to get resources (a, b, c), then for getting a's value, you have to refer to index 0, c-s value will be index 2. If you know, that c is a color, than you should perform typedArray.getColor(2, 0).
Nope, java.lang.UnsupportedOperationException: Can't convert value at index 0 to color: type=0x1
3

In Kotlin, according to @azizbekian answer, to do it you could do something like this:

val typedValue = TypedValue()
theme.resolveAttribute(com.google.android.material.R.attr.colorControlNormal, typedValue, true)
val color = ContextCompat.getColor(this, typedValue.resourceId)

Comments

1

It's correct I think, check with it

HEX

Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);

or

int color = getCurrentTextColor();
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);

Comments

1

Using Kotlin you can do the following to get the color:

val color = TypedValue().let {
    requireContext().theme.resolveAttribute(R.attr.colorControlNormal, it, true)
    requireContext().getColor(it.resourceId)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.