0

I need to format text like on the picture - some part of text must be in round rectangles like with shape background. Tried to use HTML formatting, but it does not work. Didn't find information how to implement it with span.

Any ideas? image

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
Ruslan Podurets
  • 151
  • 1
  • 2
  • 9

2 Answers2

0

you can use your custom shape drawable and use Spannable to set it in your TextView.

SpannableString ss = new SpannableString(your string); 
Drawable d = getResources().getDrawable(R.drawable.shape_drawable); 
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
textView.setText(ss); 

refer this answer for more

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • I tried. But custom shape replaces or covers text, that must be in this shape. Shape is rectangle with solid transparent color and stroke of few px. – Ruslan Podurets Apr 08 '16 at 07:03
0

You can create a drawable and set it as background for the textview

here is an example :

create a shape.xml in your drawable folder

here is a sample of code you can put in shape.xml file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="17dp" />

    <stroke
        android:width="1dp"
        android:color="#ffffff" />

    <size
        android:height="20dp"
        android:width="60dp" />

</shape>

You can make changes for color and corners as you need in this shape.xml

then set that as a background for your text view Your layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape"
        android:padding="10dp"
        android:text="@string/First_name" />

</RelativeLayout>

Here is what it looks like :

enter image description here

shadygoneinsane
  • 2,226
  • 1
  • 24
  • 47