Change the color of a TextView to only certain characters

2

I have this TextView :

<TextView
     android:id="@+id/textView17"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="10dp"
     android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dapibus sem vel magna venenatis, nec tempus enim mattis. Proin tristique faucibus nisl eget volutpat."
     android:textColor="@color/black"
     android:textSize="16sp"
     android:textStyle="bold" />

What I need is to put Lorem ipsum dolor sit amet in green and the rest in black (Default).

Is it possible to do it with code?

    
asked by Bender 01.06.2017 в 12:44
source

3 answers

3

If you want to change the color of a piece of text that you have inside the XML you can replace a piece of text with the function replace and use a SpannableString for it.

TextView textview = (TextView) findViewById(R.id.textview);
SpannableString texto = textview.getText().toString();
texto = texto.replace("Lorem ipsum dolor sit amet", "<font color=#00b300>Lorem ipsum dolor sit amet</font>");
textview.setText(Html.fromHtml(newText));
    
answered by 01.06.2017 / 13:14
source
2

I got it with this code, it's not exactly what I wanted but it works:

SpannableStringBuilder builder = new SpannableStringBuilder();

SpannableString str1= new SpannableString("Lorem ipsum dolor sit amet");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);

SpannableString str2= new SpannableString(", consectetur adipiscing elit. Aenean dapibus sem vel magna venenatis, nec tempus enim mattis. Proin tristique faucibus nisl eget volutpat.");
str2.setSpan(new ForegroundColorSpan(Color.BLACK), 0, str2.length(), 0);
builder.append(str2);

TextView tv = (TextView) dialogView.findViewById(R.id.punto1);
tv.setText( builder, TextView.BufferType.SPANNABLE);
    
answered by 01.06.2017 в 12:53
2

It can be done in various ways,

1) Using the Html.fromHtml() method and defining in strings.xml the text to change the color

 <string name="mi_mensaje"><![CDATA[Hola <font color=#FF0040>StackOverflow.com</font>, como te encuentras el día de hoy!]]></string>

in this way you make the change:

   if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        textView.setText(Html.fromHtml(getString(R.string.mi_mensaje),Html.FROM_HTML_MODE_LEGACY));
    } else {
        textView.setText(Html.fromHtml(getString(R.string.mi_mensaje)));
    }

2) Defining the text directly, using Html.fromHtml() :

String mensaje = "Hola <font color=#FF0040>StackOverflow.com</font>, como te encuentras el día de hoy!";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    textView.setText(Html.fromHtml(mensaje,Html.FROM_HTML_MODE_LEGACY));
} else {
    textView.setText(Html.fromHtml(mensaje));
}

3) Using a SpannableString , defining a Span with the desired color:

SpannableString mensaje = new SpannableString("Hola StackOverflow.com, como te encuentras el día de hoy!");
ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#FF0040"));// Puedes usar tambien .. new ForegroundColorSpan(Color.RED); 
mensaje.setSpan(colorSpan, 5, 22, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(mensaje);

The setSpan method () define as parameters:

  • The ForegroundColorSpan object.
  • The start in the string to apply the span.
  • The end of the string to apply the span.
  • Flags, in this case SPAN_INCLUSIVE_INCLUSIVE , which indicates Expand to include the text inserted in its initial or final point.

The 3 options get as a result:

similar to what you have in this answer: Bold in a part of a TextView

    
answered by 01.06.2017 в 17:08