Search code examples
androidxmlstringtextviewitalic

applying italic for item in string-array in a string xml


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.


Solution

  • 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)