1

I have two styles defined in styles.xml and styles.xml(v21). They both include a textAppearance attribute that links to a certain default android text style.

styles.xml

<style name="NotificationTitle">
    <item name="android:textAppearance">
        @android:style/TextAppearance.StatusBar.EventContent.Title
    </item>
</style>

styles.xml (v21)

<style name="NotificationTitle">
    <item name="android:textAppearance">
        @android:style/TextAppearance.Material.Notification.Title
    </item>
</style>

These default styles contain some text attributes: textColor, textSize, etc:

...
<style name="TextAppearance.Material.Notification.Title">
    <item name="textColor">@color/primary_text_default_material_light</item>
    <item name="textSize">@dimen/notification_title_text_size</item>
</style>
...

I need to get this textColor programmatically by the name of my custom style (NotificationTitle in the example). I tried to obtain it using obtainStyledAttributes, but it don't return. What should I do?

int resultColor;
int[] attrs = {android.R.attr.textColor};
TypedArray ta = context.obtainStyledAttributes(R.style.NotificationText, attrs);

if (ta != null) {
    resultColor = ta.getColor(0, Color.TRANSPARENT);
    ta.recycle();
}
2
  • make sure you are passing the right context. Commented Apr 7, 2016 at 16:14
  • The context is appropriate, I just checked - if I pass directly. android.R.style.TextAppearance_Material_Notification_Title into obtainStyledAttributes it will return the right color. But if I pass my custom style R.style.NotificationText it won't return anything. Commented Apr 7, 2016 at 16:30

1 Answer 1

1

Sorry if I'm a bit late with an answer but I tried your code with a bit of modification, I changed the style to this:

<style name="NotificationTitle" parent="TextAppearance.AppCompat.Notification.Title"/>
<style name="NotificationText" parent="TextAppearance.AppCompat.Notification.Line2"/>

and everything worked well with the rest of your code (and didn't need the v21 for the styles now that I used AppCompat)

I hope this helps.

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

2 Comments

Thanks for your answer. I just tried that old example with the changes you have proposed — and it works! For some reason, I didn't try to inherit my custom styles directly from TextAppearance.StatusBar.EventContent.Title and TextAppearance.Material.Notification.Title. Finally, the question is closed.
Glad I could help

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.