how to have bold and normal text in same textview in android?

前端 未结 7 1180
终归单人心
终归单人心 2021-02-18 23:48

I have searched internet and tried the following code but its not working

SpannableString ss1 = new SpannableString(\"Health: \");
           ss1.setSpan(new and         


        
7条回答
  •  情书的邮戳
    2021-02-19 00:33

    In kotlin, you can do this. I used this to bold characters/word within a string. For example:

    item = "Philippines"

    query = "Phil"

    result = Philippines

    val spannable = SpannableString(item)
    val indexStart = item.indexOf(query)
    val indexEnd = indexStart + query.length
    spannable.setSpan(StyleSpan(Typeface.BOLD), indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    

    and use it like this

    textView.text = spannable
    

提交回复
热议问题