Reputation: 371
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.
Upvotes: 14
Views: 46250
Reputation: 160
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.
Upvotes: 5
Reputation: 3339
Just set the TextView length as match_parent
assuming that it is nested within a parent value that spans the whole length of the screen, and your text will be the same length, but the box will increase to the whole length of the screen.
Upvotes: 0
Reputation: 61
I believe there's an easier, nicer way to go about achieving this: simply create a backgroundColorSpan and add it to a SpannableString. Something along those lines:
public static SpannableString buildBackgroundColorSpan(SpannableString spannableString,
String text, String searchString, int color) {
int indexOf = text.toUpperCase().indexOf(searchString.toUpperCase());
try {
spannableString.setSpan(new BackgroundColorSpan(color), indexOf,
(indexOf + searchString.length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
}
return spannableString;
}
Where "spannableString" is the SpannableString object created and assumed to be initialised with "text"; "searchString" represents the piece of text you wish to "highlight" in your TextView, and "color" the background colour to which the "highlighted" text should be set.
String text = "The Quick Brown Fox Jumps over the Lazy Dog";
String searchString = "brown";
int color = Color.parseColor("#FFF5F19E");
SpannableString spannableString = new SpannableString(text);
spannableString = StringUtils.buildBackgroundColorSpan(spannableString, text, searchString, color);
I think this should suffice.
Thanks
Upvotes: 5
Reputation: 21
If you want to do it from XML try the solution of the other people who just answered like:
<TextView
android:id="@+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:background="#1bgtbgt"/>
If you want to do it from Activity Code you can change it dynamically like try with:
text.setBackgroundColor( int color);
Upvotes: 1
Reputation: 1
You could add another layout around the TextView and set its background to the colour you want
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#FFFFFF"
android:text="@string/text" />
</LinearLayout>
Upvotes: -1
Reputation: 69
Try this below the textview
in .xml
file:
<TextView android:background="#0000FF">
Upvotes: 0
Reputation: 368
Try Using this for text background color
<TextView
android:id="@+id/comment_dis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Stack Overflow
android:textColor="#ffffff"
android:background="#3cabdc"/>
Upvotes: -1
Reputation: 567
Use this example
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000FF"
android:textColor="#FFFFFF" />
for picking color use this link http://html-color-codes.info/
Red:#750A0A
Blue:#002BFF
Green:#33FF00
White:#FFFFFF
Black:#000000
Upvotes: 1
Reputation: 2629
<TextView
android:background="#0000FF"
android:textColor="#FFFFFF" />
Would define a TextView
with a blue background and white text...is that what you need?
Upvotes: 17