Reputation: 4566
I have the following code, thanks to the help of @TangXianQiang which creates a background that is wrapped tightly around the text in a TextView:
String str="this bit of text spans more than one line.Words words words";
int bstart=0;
int bend=str.length();
SpannableStringBuilder style=new SpannableStringBuilder(str);
style.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this,R.color.colorGreenTra)),bstart,bend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tvColor=(TextView) findViewById(R.id.tv_color);
tvColor.setText(style);
and in XML:
<TextView
android:id="@+id/tv_color"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="this bit of text spans more than one line.Words words words"
/>
However, I would like to add 8dp of padding inside this background so that it does not hug the edge of the text so tightly. How could I accomplish this since the string builder is only working with the start and end of the string?
Upvotes: 0
Views: 1153