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