I thought that the selected answer did not give a satisfactory result. I wrote my own function, which takes 2 lines; Full text and part of the text you want to make bold.
It returns a SpannableStringBuilder with the text textToBold from the text in bold.
I find it possible to make a substring in bold without using it in tags.
/** * Makes a substring of a string bold. * @param text Full text * @param textToBold Text you want to make bold * @return String with bold substring */ public static SpannableStringBuilder makeSectionOfTextBold(String text, String textToBold){ SpannableStringBuilder builder=new SpannableStringBuilder(); if(textToBold.length() > 0 && !textToBold.trim().equals("")){ //for counting start/end indexes String testText = text.toLowerCase(Locale.US); String testTextToBold = textToBold.toLowerCase(Locale.US); int startingIndex = testText.indexOf(testTextToBold); int endingIndex = startingIndex + testTextToBold.length(); //for counting start/end indexes if(startingIndex < 0 || endingIndex <0){ return builder.append(text); } else if(startingIndex >= 0 && endingIndex >=0){ builder.append(text); builder.setSpan(new StyleSpan(Typeface.BOLD), startingIndex, endingIndex, 0); } }else{ return builder.append(text); } return builder;
}
source share