34990

android:textColor no longer works in Marshmallow

Question:

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 res/values/styles.xml:

<color name="orvGyro">#33B5E5</color>

Layout:

<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:

accStatus.setTextColor(getResources().getColor(R.color.somecolor));

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.

accStatus.setTextColor(ContextCompat.getColor(context, R.color.somecolor)); <blockquote>

<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

</blockquote>

Answer2:

Got it.

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.

Recommend

  • How to choose target API level when creating a new project in Android Studio?
  • How to run an R script and show a plot?
  • Android N FileUriExposedException
  • Batch script file name
  • Get Internal and External Memory size in MarshMallow
  • Don't work access to torch in android marshmallow
  • Unable to insert form data in MySQL database
  • How to choose MappingContext in spring-data-jpa (2x) + spring-rest-webmvc?
  • Failed running an app as root
  • Open id and facebook implementation in php?
  • Crontab issues with python script
  • Can't find mobile device when using Web Bluetooth API
  • Can we mix JSF RI 1.1 and Tamhawk and primefaces,etc in one application
  • Can I determine HTML5 support in my users’ browsers with Google Analytics?
  • Updating Entity Framework EDMX Models After Database Migration
  • Migration of Google Project to Firebase Console
  • Use OAuth2 for authentication + compatibility with google.appengine.api.users service
  • Acts as Tree with Multiple Models
  • Can't get LogCat (Alcatel OneTouch Evolve)
  • Division with Aggregate Functions in SQL Not Behaving as Expected
  • Keep same scale in different graphs ggplot2
  • update record in database using jdatabase
  • Vue props data not updating in child component
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • CloseOptionsMenu doesn't work?
  • Laravel 4 routing not working due to .htaccess file?
  • How to split circle in to the sectors in google maps?
  • Implicit joins and Where in Doctrine - how?
  • Button text different than value submitted in query string
  • Array with custom indexes in Ionic2
  • Textfile Structure (tables)
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Database structure design with variable amounts of fields
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Cross-Platform Protobuf Serialization
  • How to format a variable of double type
  • log4net write single file for each call to log.info
  • coudnt use logback because of log4j
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • JaxB to read class hierarchy