0

I have a string array which contains couple of paragraphs .one word contains a italic font. How can i apply italic font for a particular word-textview . I know we can apply italic using spannable. but any other way?

<string-array name="string_collections" formatted="false">
<item>nutrients and weakens the <![CDATA[<i>Agni</i>]]>(digestive fire) within the stomach. Ayurveda recommends sipping minimal amounts during meals, with larger volumes spaced throughout the day, always away from food.
 \n</item>

It is showing as text. no effect using Html.fromHtml method.

Sree Reddy Menon
  • 1,301
  • 13
  • 23

2 Answers2

1

Try this one :-

Try Html.fromHtml(), and mark up your text with bold and italic HTML tags e.g:

Spanned text = Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff");
textView.setText(text);
Nik Patel
  • 627
  • 7
  • 21
0

I found a better approch my self.just use Matcher and get index of it.

     java.util.regex.Pattern p =
                                    java.util.regex.Pattern.compile("(^|\\s)word\\b",
                                            java.util.regex.Pattern.CASE_INSENSITIVE);
                   final Matcher matcher = p.matcher(whole_text);


        final SpannableStringBuilder spannable = new SpannableStringBuilder(whole_text);
                            while (matcher.find()) {
                                Log.e("Spannable", "Spannable" +"Matchfound");
                      //apply span which you want.
 spannable.setSpan(new StyleSpan(Typeface.ITALIC), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            }
                            tvtextmsg.setText(spannable);

Note:just found some another issue i have faced while using if you are trying to do as below

 tvtextmsg.setText(spannable+"\n \n"+another_text);

spannable will not be effected .you need do as

tvtextmsg.setText(spannable);
tvtextmsg.append("\n \n"+another_text)
Sree Reddy Menon
  • 1,301
  • 13
  • 23