Android: Coloring Part of a String Using Textview.Settext()

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);

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);

Set color of TextView span in Android

Another answer would be very similar, but wouldn't need to set the text of the TextView twice

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);

How to set color on string

Try as follow

String textFirstPart = start + "....";
String textSecondPart = "read more";
String text = textFirstPart + textSecondPart;
Spannable spannable = new SpannableString(text);
spannable.setSpan(new ForegroundColorSpan(Color.RED), textFirstPart.length(),
(textFirstPart + textSecondPart).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.Description.setText(spannable, TextView.BufferType.SPANNABLE);

How to color part of TextView in android?

this will help u

Spannable WordtoSpan = new SpannableString("I know just how to whisper");        
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 13,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(WordtoSpan);

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 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);

Set TextView's portion of text color?

Yesterday i have faced same problem with Html.fromHtml() so i have used spannable string here is how i will edit your code

SpannableStringBuilder builder = new SpannableStringBuilder();

SpannableString carOwnerSpannable= new SpannableString("Mayuri");
carOwnerSpannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(JobDetailTabbedActivity.this,R.color.colorAccent)), 0, carOwnerName.length(), 0);
builder.append(carOwnerSpannable);

builder.append(" | LastName:Joshi");

textView.settext(builder);

comment below if this doesn't work.

Expected Output:

Mayuri(in green color(my color accent is green)) | LastName:Joshi(In white)

add <color name="colorAccent">#1AB394</color> to colors.xml

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);
}

Coloring part of the text in an android TextView with a color defined in an xml file

I solved my problem myself with regex:

public static Spanned formatTextForTextView(String text, Context context){
text = replaceColorForHex(text, "blue", R.color.blue, context);
text = replaceColorForHex(text, "green", R.color.green, context);
text = replaceColorForHex(text, "red", R.color.red, context);
return Html.fromHtml(text);
}

private static String replaceColorForHex(
String text,
String colorString,
int colorId,
Context context
){
String colorHex = getColorHex(colorId, context);

Pattern pattern = Pattern.compile(
"(<font[^>]+color=)(['" + '"' + "]"
+ colorString
+ "['" + '"' + "])([^>]*>)"
//Captures a <font color='colorString'> html tag.
//Allows for additional attributes within the font
//tag
);
Matcher matcher = pattern.matcher(text);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(
sb, matcher.group(1) + colorHex + matcher.group(3));
}
matcher.appendTail(sb);
text = sb.toString();
return text;
}

private static String getColorHex(int colorId, Context context){
int colorContent = context.getResources().getColor(colorId);
return String.format("#%06X", (0xFFFFFF & colorContent));
}


Related Topics



Leave a reply



Submit