6

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mehboob Maniyar
  • 238
  • 1
  • 3
  • 11

2 Answers2

12

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"
Ivano
  • 25
  • 5
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • 2
    using this am able to give background color but not possible the rounded background.How can I give the corners to background? – Mehboob Maniyar Sep 08 '14 at 12:31
  • 1
    Hey I tried that but its showing rounded background to textView(means all strings in textView) but i want it to only String not complete textView...Thanks – Mehboob Maniyar Sep 08 '14 at 12:50
  • 1
    @ InnocentKiller can you get some clue from this link. http://stackoverflow.com/questions/19292838/android-spannablestring-set-background-behind-part-of-text – Mehboob Maniyar Sep 08 '14 at 12:52
1

I have achieved the above UI like this.. Created Horizontal scroll-view and one Linear Layout with horizontal orientation. next during run-time depending upon the No of Strings in array i have added text view in Linear Layout that's it. The code is as below.

    private void addTextView(ArrayList<String> list,String whichLayout){

    for (int i = 0; i < list.size(); i++) {
         TextView textView = new TextView(getActivity());
         Spannable spValue = new SpannableString(list.get(i).toString());
         textView.setText(customeSpan.getRequiredFontTypeToText(spValue, tfHintTxtValue));
         textView.setTextSize(12.0f);
         textView.setTextColor(getResources().getColor(R.color.black));
         textView.setBackgroundResource(R.drawable.red_solid_background);
         LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         lllp.setMargins(0, 2, 10, 0); // llp.setMargins(left, top, right, bottom);
         textView.setLayoutParams(lllp);
         if(whichLayout.equalsIgnoreCase("hieghtWieght")){
             ((LinearLayout) heightWieghtLinearLayout).addView(textView);
         }else if(whichLayout.equalsIgnoreCase("bodyType")){
             ((LinearLayout) bodyTypeLinearLayout).addView(textView);
         }else if(whichLayout.equalsIgnoreCase("eyeHair")){
             ((LinearLayout) eyeHairColorLinearLayout).addView(textView);
         }else if(whichLayout.equalsIgnoreCase("bestFeatures")){
             ((LinearLayout) bestFeaturesLinearLayout).addView(textView);
         }else if(whichLayout.equalsIgnoreCase("personalStyle")){
             ((LinearLayout) personalStyleLinearLayout).addView(textView);
         }else if(whichLayout.equalsIgnoreCase("zodiacSign")){
             ((LinearLayout) zodizcSignLinearLayout).addView(textView);
         }else if(whichLayout.equalsIgnoreCase("personalityTraits")){
             ((LinearLayout) personalityLinearLayout).addView(textView);
         }  
    }
} 
Mehboob Maniyar
  • 238
  • 1
  • 3
  • 11