0

So i have this little text view

<TextView
        android:id="@+id/textDescription"
        android:layout_width="102dp"
        android:layout_height="34dp"
        android:layout_marginTop="8dp"
        android:fontFamily="@font/open_sans_bold"
        android:text="Red"
        android:textSize="21sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/addclass_photo" />

And i just want to change the letter "R" to red on android:text:"Red"

1 Answers1

1

There are several ways to achieve this.

    TextView textView = findViewById(R.id.textDescription);

    String firstChar = "<font color='#EE0000'>R</font>";
    String str = "ed";
    textView.setText(Html.fromHtml(firstChar + str));
    // or
    textView.setText(Html.fromHtml("<font color='#EE0000'>R</font>ed"));

Or in kotlin:

val textView = findViewById<TextView>(R.id.textDescription)
val firstChar = "<font color='#EE0000'>R</font>"
val str = "ed"
textView.text = Html.fromHtml(firstChar + str)
// or 
textView.text = Html.fromHtml("<font color='#EE0000'>R</font>ed")

Result:

R_ed text

This question is about changing one word/substring and contains other ways to do this: Change word color from text view

some user
  • 1,693
  • 2
  • 14
  • 31