How can I achieve such an effect with an Android TextView. It looks somehow like selected text and I couldn't find something similar in the API.
This is not a background color for the view, but a background color only for the text. You can see how it stops at line breaks and has a thin white line between text lines.
Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.
<TextView
android:background="#0000FF"
android:textColor="#FFFFFF" />
Would define a TextView
with a blue background and white text...is that what you need?
As far as I can see, there's no 'nice' way of doing this without overriding TextView and drawing custom paints on the view which includes the gap colour.
Even setting the lineSpacingExtra
property only expands the background colour.
You could also potentially look into creating a custom spannable
and use it like
Spannable str = new SpannableStringBuilder("How can I achieve such an effect with an Android TextView. It looks somehow like selected text and I couldn't find something similar in the API.");
str.setSpan(new NewSpannableClass(), 0, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
((TextView)findViewById(R.id.text)).setText(str);
Where NewSpannableClass
is the custom spannable.
Seeing as many people are lazy to look up how to make custom spannables, here's an example
public class CustomSpannable extends ClickableSpan
{
@Override public void updateDrawState(TextPaint ds)
{
super.updateDrawState(ds);
ds.setUnderlineText(true);
}
}
This example will underline the text. Use TextPaint
to change the look of the spanned text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With