Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background color to spanableString in android

I am using only one text view and using spanable to add fonts and color to strings. But i am getting problem to display rounded background to string using spanable. So How can I achieve this using spannable String concept the image is as shown below.

enter image description here

like image 617
M Art Avatar asked Sep 08 '14 12:09

M Art


Video Answer


1 Answers

For the background color of your TextView you can use

String myString = "myString";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(0xFFCCCCCC),0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);           
myTextView.setText(spanna);

and for rounded text-view create your custom XML and set background to that textview.

Edit

Create one XML called rounded_corner.xml and set this content

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- view background color -->
    <solid android:color="#ffffff" >
    </solid>

    <!-- view border color and width -->
    <stroke
        android:width="1dp"
        android:color="#1c1b20" >
    </stroke>

    <!-- If you want to add some padding -->
    <padding
        android:bottom="4dp"
        android:left="4dp"
        android:right="4dp"
        android:top="4dp" >
    </padding>

    <!-- Here is the corner radius -->
    <corners android:radius="10dp" >
    </corners>

</shape>

and then add this line to your textview in XML

android:background="@drawable/rounded_corner"
like image 78
InnocentKiller Avatar answered Sep 21 '22 14:09

InnocentKiller