1

When using the ForegroundColorSpan on a piece of text instead of changing the text color it will underline it when the cursor is on top of it. This happens on my (Physical) Pixel 3 running Android 9 (Pie) and on a (Physical) Pixel 1 also running Android 9 (Pie).

However, when I run it on a (Virtual) FWVGA running Android 7.1.1 (Nougat) it turns up as expected.

I have looked all around but I'm not able to find anything in regards to this (possibly because Android 9 is relatively new?)

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val editText = findViewById<EditText>(R.id.codeView)

        var ss: SpannableStringBuilder = SpannableStringBuilder("I'm RED and I'm GREEN now")

        //val testing123: Spanned = Html.fromHtml("<u><font color='#2eb6f0'>Blue I Hope</font></u>")
        val fcsRed: ForegroundColorSpan = ForegroundColorSpan(Color.RED)
        val fcsGreen: ForegroundColorSpan = ForegroundColorSpan(Color.GREEN)

        ss.run {
            setSpan(fcsRed, 3, 7, 0)
            setSpan(fcsGreen, 16, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        editText.setText(ss)


    }
}

activity_main.xml -> codeView

<EditText
            android:id="@+id/codeView"
            android:text = ""
            android:layout_width="362dp"
            android:layout_height="503dp"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:gravity="top|left"
            android:inputType="textMultiLine"
            android:lines="40"
            android:maxLines="20000"
            android:minLines="1"
            android:scrollbars="vertical|horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.956" />

(Physical) Pixel 3 running Android 9 (Pie) (Physical) Pixel 3 running Android 9 (Pie)

(Virtual) FWVGA running Android 7.1.1 (Nougat) (Virtual) FWVGA running Android 7.1.1 (Nougat)

  • I tested your code in 2 Android P emulators of AS and it works fine. – forpas Nov 06 '18 at 15:35
  • Do you have any idea why I'm getting the result I am on my Pixel phones? I'm about to factory reset my old one to test if it's a setting I changed – Jeremy Weisener Nov 06 '18 at 15:55
  • No I don't. I reproduced your code and tried it over and over because I use a lot of spannable strings in my app and it all works fine. Maybe it's a device issue (I hope). – forpas Nov 06 '18 at 16:15
  • It was a setting I tweaked. "High Contrast Text" -- I submitted an answer to it below because I feel it might help someone else down the road. When you said you also tested on an Android P I started thinking it was a setting I changed so I started looking. --- Thank you for all your help ! – Jeremy Weisener Nov 06 '18 at 16:19
  • That's good news. – forpas Nov 06 '18 at 16:22
  • @JeremyWeisener Hi, did you find a proper solution for this? It seems that lots of Samsung devices after updating to Android Pie automatically switch on High Contrast text which is causing the color issues. – zeke Apr 16 '19 at 08:00
  • @zeke I answered my own question below but didn't mark it as correct. It was a setting for "High Contrast Text". Please see the answer below – Jeremy Weisener Apr 16 '19 at 11:03
  • @JeremyWeisener I saw it, but that's not exactly a solution, you can't make all users change this setting. – zeke Apr 16 '19 at 11:20
  • @zeke sorry, I'm not much of an Android programmer, I was just doing this for fun a little. --- check out https://stackoverflow.com/questions/37402880/enabling-high-contrast-text-mode-from-within-android-java-using-accessibilitym --- seems to have the most info – Jeremy Weisener Apr 16 '19 at 11:36
  • @JeremyWeisener OK, I see, thanks. – zeke Apr 16 '19 at 11:44

1 Answers1

0

Settings > Accessibility > Experimental > High contrast text

After Factory Resetting my Pixel 1 (and the app working as it should) I figured out it must have been a setting I had turned on. After looking through all my settings I figured out that it was High Contrast Text that was causing the font color to default to black and only show the color as the underline when the cursor was over it.