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

前端 未结 7 1182
终归单人心
终归单人心 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:32

    I am bit late to answer, but I created a method for easy use by using answer already provided here.

        private void setSpanString(String string1, String string2, TextView textView) {
        SpannableStringBuilder builder=new SpannableStringBuilder();
        SpannableString txtSpannable= new SpannableString(string1);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        txtSpannable.setSpan(boldSpan, 0, string1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.append(txtSpannable);
        builder.append(string2);
       textView.setText(builder, TextView.BufferType.SPANNABLE);
    }
    

提交回复
热议问题