Skip to content Skip to sidebar Skip to footer

Android Setbackground Color For Custom Text View

I m trying to set background color text view to length of the text in that text view , if I use textView.SetBackground() then it will set background color to whole textview , i d

Solution 1:

Set the TextView's layout_width to wrap_content, then the background will only be as lasrge as the content.

Solution 2:

use SpannableString class

here is example

StringbtnText= btn.getText();
SpannableStringspannableSummary=newSpannableString(btnText);
spannableSummary.setSpan(newForegroundColorSpan(0xff6083f0), 0, summary.length(), 0);

btn.setText(btnText);

Solution 3:

Solution 4:

You can use

String text = "This is <fontcolor='blue'>blue</font>. This is <fontcolor='red'>red</font>.";
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

Post a Comment for "Android Setbackground Color For Custom Text View"