5

The text color of a TextView is always white on pre-Marshmallow devices. Even if I am using yellow or black in my XML, it is always white except on Marshmallow.

At run time it will work fine on all devices. Previously, when I was using Eclipse, everything was working find but in Android Studio it is not taking the attribute from the XML.

All of the questions I have searched say to change Theme.AppCompat to Theme.AppCompat.Light but this does nothing except change white to black. I believe there is a style mistake. Here is my style code:

 <style name="AppBaseTheme" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/khaki</item>
        <item name="colorPrimaryDark">@color/khaki</item>
        <item name="colorAccent">@color/my_yellow</item>

        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

Here is my xml

 <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="48dp">
   <ImageView
    android:id="@+id/icon1"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
     android:src="@drawable/sj_icon"
    android:layout_centerVertical="true"/>

   <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/icon1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="32dp"
    android:gravity="center_vertical"
    android:minHeight="34dp"
    android:paddingLeft="7dp"
    android:text="hello"
    android:textColor="@color/my_yellow"
    android:textSize="18sp" />

   </RelativeLayout>
Mangesh
  • 5,491
  • 5
  • 48
  • 71
Jishant
  • 574
  • 3
  • 14
  • In your layout's XML file, how are you setting the text color? Are you using `android:textColor="@color/colorPrimary"` inside the `` tag? – AdamMc331 Mar 14 '16 at 14:39
  • 1
    Same problem after android studio update... In new project works fine, in old project textColor not working. – Hagakurje Mar 14 '16 at 21:09
  • @McAdam.... I am using android:textColor="@color/my_yellow" or android:textColor="#000000" black color but I am still getting white color in device.. – Jishant Mar 15 '16 at 05:27
  • Possible duplicate of http://stackoverflow.com/questions/35890832/how-to-change-the-text-color-in-android-action-bar/35890901#35890901 – Jay Rathod Mar 15 '16 at 05:39
  • @Ancee i think you have values-v21 folder in your project? – balaji Mar 15 '16 at 06:14
  • @ jaydroider that is for actiobar color... and i also added Your Color Here but it is still not working – Jishant Mar 15 '16 at 06:15
  • @ jaydroider.. i can guess in values textcolor is hardcode to white ... as i change my theme to Theme.AppCompat.Light it is changing to black color .. but where it is happening i dunno.. – Jishant Mar 15 '16 at 06:18
  • @Ancee You mean yellow color is not showing at design mode instead of it is showing white at design mode always. – Jay Rathod Mar 15 '16 at 06:20
  • @ jaydroider ... no in design it is showing yellow ..but at run time in device it is always showing white.. but in marshmalo device it is working fine... – Jishant Mar 15 '16 at 06:23
  • @Ancee You mean problem is only come to lower versions then marshmallow. Are you testing in real device or emulator ? – Jay Rathod Mar 15 '16 at 06:26
  • @ balaji... I am hust using value folder.. no 21,23 or any other value folder... – Jishant Mar 15 '16 at 06:28
  • @jaydroider.. yes in lower devices.. and using real devices sir.. – Jishant Mar 15 '16 at 06:29
  • @Ancee Try by setting text color programetically this might be solve your probelm. Do this yourTextView.setTextColor(getResources().getColor(R.color.white)); – Jay Rathod Mar 15 '16 at 06:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106315/discussion-between-ancee-and-jaydroider). – Jishant Mar 15 '16 at 06:39
  • @Ancee tell me your colorPrimary , colorPrimaryDark color code? – balaji Mar 15 '16 at 06:40
  • 1
    Are you using Stable or Beta version of Android Studio? I experienced similar strange issues in Beta versions of Android Studio. If you are using Beta version please try to use a Stable version. Also sometimes renaming the resource names do the trick for me in such scenarios. So try to rename your xml layout and see whether it working – dishan Mar 15 '16 at 07:39

2 Answers2

1

I finally got the answer of my problem actually I was using latest version of support library that was creating problem I compare everything with my colleague project as his app was working fine..
use this

compile 'com.android.support:appcompat-v7:23.1.1'

studio will give popup a newer version is also available but don't use that as 24 version is in alpha stage.

Jishant
  • 574
  • 3
  • 14
1

I faced a problem similar to this (in a widget, the text color was white if the average of RGB values is more than half the max value and black if not)

I was testing the app in a phone having the "high contrast text" feature enabled, so the solution was disabling it.

Abdo Kamal
  • 76
  • 4