I have written an app which relies on colors defined in resources. Some are set directly in the layout XML file, others are set in code. Examples:
Color definition in
<TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dotSpace" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/orvGyro" />
Color in code:
The app targets API 17. Up to Lollipop, this has worked flawlessly, displaying the right colors. After migrating to Marshmallow (Cyanogenmod 13), all these colors display as orange. Other colors, which are defined in Java code and not in resources, seem to display correctly.
I've tried changing the target API to 23 and adding styles for API 21+, to no avail.
What's wrong here? Is that a bug in CyanogenMod13, or am I doing something wrong?
<strong>EDIT:</strong> It seems it's not about getting the color from the resource. Hard-coding the colors as shown below also gives me orange text:
<TextView android:id="@+id/textView9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dotSpace" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#669900" />
<strong>EDIT 2:</strong> Just came across <a href="https://stackoverflow.com/questions/32394723/android-m-developer-preview-textview-androidtextcolor-being-ignored" rel="nofollow">Android M Developer Preview - TextView android:textColor being ignored</a>. Could this explain the behavior I'm experiencing?
<strong>EDIT 3:</strong> When I generate content dynamically instead of using layouts, colors display correctly. Example:
TextView newType = new TextView(rilLteCells.getContext()); newType.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2)); newType.setTextAppearance(rilLteCells.getContext(), android.R.style.TextAppearance_Medium); newType.setTextColor(rilLteCells.getContext().getResources().getColor(getColorFromGeneration(cell.getGeneration()))); newType.setText(rilLteCells.getContext().getResources().getString(R.string.smallDot)); row.addView(newType);Answer1:
Use <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html" rel="nofollow">ContextCompat</a> class, It is helper class for accessing features in Context introduced after API level 4 in a backwards compatible fashion.
<a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getColor%28android.content.Context,%20int%29" rel="nofollow">
public static final int getColor (Context context, int id)</a>
Returns a color associated with a particular resource ID
Wherever I had this issue, the text displayed in the control was a single square (U+2b1b). When I alter this text (e.g. by appending an X), only the square will display in orange and the rest of the string has the desired color.
Changing it to a small square (U+25fc) fixed things. Some other special characters would give me other colors – apparently certain characters are fixed to certain colors on Marshmallow, when on earlier versions they could be styled like any other text.Answer3:
Faced the same problem on my Sony Xperia (Android 6.0 Marshmallow). The reason was that the Settings/Accessibility/High contrast text (experimental) was enabled.
When I disabled it, it worked fine again as expected.