0

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"
    />

The result: enter image description here

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?

Paradox
  • 4,602
  • 12
  • 44
  • 88
  • @pskink I have already tried doing this and it does not affect the background. – Paradox Mar 06 '18 at 03:08
  • 1
    Have a look at https://www.google.co.in/url?sa=t&source=web&rct=j&url=https://medium.com/%40tokudu/android-adding-padding-to-backgroundcolorspan-179ab4fae187&ved=2ahUKEwiVm9L33tbZAhWBOI8KHR1mAyIQFjABegQIBxAB&usg=AOvVaw02Yg_bdaO045_GaSVhrAve – ADM Mar 06 '18 at 03:22
  • Did u find a solution for this issue ? – A. Albrg Sep 22 '21 at 12:52

0 Answers0