0

This is my code

 TextView text_language_name4 = (TextView)view.findViewById(R.id.text);
                     if (text_language_name4 != null) {
                         int text_color4 = selected
                                 ? getResources().getColor(R.color.readcolor)
                                         : getResources().getColor(R.color.readcolor);                
                                 text_language_name4.setTextColor(text_color4);
                         text_language_name4.setDuplicateParentStateEnabled(true);

                 }

When i am using R.attr.mytheme my app forces close

Any suggestions for using attr to change color of a text view

NITIN DUDA
  • 192
  • 1
  • 14

3 Answers3

4

For the theme color try like this:

TypedValue tV = new TypedValue();
Theme theme = context.getTheme();
boolean success = theme.resolveAttribute(R.attr.theme_color, tV, true);
int colorFromTheme;
if(success)
    colorFromTheme = tV.data;
else
    // value not found....

Now set,

textView.setTextColor(colorFromTheme);
Mohammed Ali
  • 2,758
  • 5
  • 23
  • 41
0

You can use something like this if i understood your problem right:

textView.setTextColor(Color.parseColor("#ffffff"));

While "#ffffff" is the hex value of the color.

Seem
  • 11
  • 1
-1

Assuming you have:

<color name="green">#0000ff00</color>

And here is code:

int greenColor = getResources().getColor(R.color.green);
 String strGreenColor = "#"+Integer.toHexString(greenColor);

mTextView.setTextColor(Color.parseColor(""+greenColor));
  • Thank you for the suggestion but i want to get colors from my theme and when i am doing such thing my app forces close getResources().getColor(R.attr.mytheme) : getResources().getColor(R.attr.mytheme); – NITIN DUDA Nov 19 '14 at 12:16
  • whats the error that force closes the app? the above ans is correct, you can use it... – Mohammed Ali Nov 19 '14 at 12:20
  • this is not what i am asking , i am asking if i can use attr for example getResources().getColor(R.attr.green); I have 2 themes in my app and both have different color . – NITIN DUDA Nov 19 '14 at 12:27