Skip to content Skip to sidebar Skip to footer

Android Textview Text Background Color

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

Solution 1:

<TextView
android:background="#0000FF"android:textColor="#FFFFFF" />

Would define a TextView with a blue background and white text...is that what you need?

Solution 2:

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

publicclassCustomSpannableextendsClickableSpan
{
    @OverridepublicvoidupdateDrawState(TextPaint ds)
    {
        super.updateDrawState(ds);
        ds.setUnderlineText(true);
    }
}

This example will underline the text. Use TextPaint to change the look of the spanned text.

Solution 3:

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:

publicstatic 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.

Stringtext = "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

Solution 4:

Use this example

<TextViewandroid: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

Solution 5:

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

Post a Comment for "Android Textview Text Background Color"