1

I would like to highlight a TextView and achieve the design shown below. Does anyone know how to achieve this using a TextView? I took this screenshot from an existing Android app.

enter image description here

By using this code I get results shown below, which is not what I want:

sp.setSpan(new BackgroundColorSpan(color), start, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

enter image description here

1

2 Answers 2

3

You need to use a Spannable String:

TextView textView = (TextView)findViewById(R.id.textView1);
String  text = "Test"; 
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);

Try this. This will work.

Sign up to request clarification or add additional context in comments.

4 Comments

Please check my updated question, BackgroundColorSpan results in something different from what I really want.
What about letting the style apart from the UI?
@Tinashe Did you ever get an answer? The answers above do not address your question about the needed white space. I have the same problem and have not been able to find an answer.
@BobLissner Unfortunately I could not find an answer. But I assume this can only be achieved with css
1

If I understand you correctly, you want to highlight the text with a light color? If that's the case, then simply change the transparency of the color. For example: change the first two FF of 0xFFFFFF00 to something like 80(50 % transparency). So, it would look like 0x80FFFF00.

TextView textView = (TextView)findViewById(R.id.textView1);
String  text = "Your String"; 
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(new BackgroundColorSpan(0x80FFFF00), 14, 19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);

Here's a relevant thread regarding hex color transparency.

1 Comment

this is way better than what I had, Thanks a lot. but still it does not leave the white spaces in-between the sentences as it is in the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.