How to Change the Color of a Part of a Textview

How can I change the color of a part of a TextView?

Spannable is more flexible:

String text2 = text + CepVizyon.getPhoneCode() + "\n\n"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText();

Spannable spannable = new SpannableString(text2);

spannable.setSpan(new ForegroundColorSpan(Color.WHITE), text.length(), (text + CepVizyon.getPhoneCode()).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

myTextView.setText(spannable, TextView.BufferType.SPANNABLE);

How to change text color of the part of the TextView?

Try This:

Set TextView as a HTML using SpannableTextView

String text = "<font color='black'>123456</font><font color='red'>789</font>";
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

Change the color of a part of a TextView in strings.xml

Use <font color="#008000">free</font> instead. According to the documentation, the correct attribute name is color and it only supports hex codes.

Color part of string in textView in XML

I've tried with your given string and it is works for me. Here is an example

  • Create <TextView> inside any layout

    <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text" />

  • Create text string inside strings.xml file

    <string name="text"><font color="#00ff00">Hello</font> <font
    color="#FF0000">World</font></string>

  • And you can see the result

    Sample Image

Android change color part of a string

Use ForegroundColorSpan.

if(classe != null && classe.contains(“idiom”))
{
Spannable spannable = new SpannableString(classe);
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), classe.indexOf(“idiom”), classe.indexOf(“idiom”) + “idiom”.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtclasse.setText(spannable);
}

Change color and make bold part of a TextView in xml

<b>Log In</b> will make it. The string resource is to be as follows:

<string name="already_have_an_account">Already have an account? <font color='#FF6200EE'><b>Log In</b></font></string>

Here is more on Styling with HTML markup.

Android: Coloring part of a string using TextView.setText()?

Use spans.

Example:

final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");

// Span to set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158));

// Span to make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);

// Set the text color for first 4 characters
sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

// make them also bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

yourTextView.setText(sb);

How to set different color to some text of a textiew and make that text clickable?

Using setHighlightColor() method works most of the time:

textView.setHighlightColor(ContextCompat.getColor(context, R.color.green));

NOTE: Updated code to 2 different strings into 1 TextView and Second string will be coloured and clickable.

Set your TextView's default color as BLACK
Clickable part of will be GREEN

There is a simple example:

String stringFirst = "..."
String stringSecond = "..."

SpannableString spannable = new SpannableString(stringFirst + stringSecond);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}

@Override
public void onClick(View widget) {
//Do your click action
}
};
spannable.setSpan(clickableSpan, stringFirst.length()-1, stringFirst.length() + stringSecond.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(spannable);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(ContextCompat.getColor(context, R.color.green));

If it's not work add the line below

ds.setColor(ContextCompat.getColor(context, R.color.green));

to your updateDrawState method. It looks like this:

String stringFirst = "..."
String stringSecond = "..."

SpannableString spannable = new SpannableString(stringFirst + stringSecond);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(ContextCompat.getColor(context, R.color.green));
}

@Override
public void onClick(View widget) {
//Do your click action
}
};
spannable.setSpan(clickableSpan, stringFirst.length()-1, stringFirst.length() + stringSecond.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(spannable);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(ContextCompat.getColor(context, R.color.green));

How to set the text color of TextView in code?

You should use:

holder.text.setTextColor(Color.RED);

You can use various functions from the Color class to get the same effect of course.

  • Color.parseColor (Manual) (like LEX uses)

    text.setTextColor(Color.parseColor("#FFFFFF"));
  • Color.rgb and Color.argb (Manual rgb) (Manual argb) (like Ganapathy uses)

    holder.text.setTextColor(Color.rgb(200,0,0));
    holder.text.setTextColor(Color.argb(0,200,0,0));
  • And of course, if you want to define your color in an XML file, you can do this:

    <color name="errorColor">#f00</color>

    because the getColor() function is deprecated1, you need to use it like so:

    ContextCompat.getColor(context, R.color.your_color);
  • You can also insert plain HEX, like so:

    myTextView.setTextColor(0xAARRGGBB);

    Where you have an alpha-channel first, then the color value.

Check out the complete manual of course, public class Color extends Object.


1This code used to be in here as well:

textView.setTextColor(getResources().getColor(R.color.errorColor));

This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.

Change text color of one word in a TextView

Easiest way I know is to just use html.

String first = "This word is ";
String next = "<font color='#EE0000'>red</font>";
t.setText(Html.fromHtml(first + next));

But this will require you to rebuild the TextView when (if?) you want to change the color, which could cause a hassle.



Related Topics



Leave a reply



Submit