I am making an app that requires the Text Formatting Features such as making the selected text Bold, Italic, Strike through, Underline, etc. I have used the StyleSpan(Typeface.BOLD), StyleSpan(Typeface.Italic), UnderlineSpan(), StrikethroughSpan() provided by android. All these are working fine. But, when I try to apply a new span over a previous span, the previously applied span is removed and the new one is applied. Here is the function that I am using to apply the format based upon the button click :
fun formatText(typefaceCode: StyleSpan = StyleSpan(Typeface.BOLD), isUnderline : Boolean = false, isStrikeThrough : Boolean = false, isQuote : Boolean = false){
val selectionStart: Int = mEditTextNoteContent.selectionStart
val selectionEnd: Int = mEditTextNoteContent.selectionEnd
val selectedText: String = mEditTextNoteContent.text.toString().substring(selectionStart, selectionEnd)
val formattedString = SpannableStringBuilder(selectedText)
when {
isUnderline -> formattedString.setSpan(UnderlineSpan(), 0, selectedText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
isStrikeThrough -> formattedString.setSpan(StrikethroughSpan(), 0, selectedText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
isQuote -> mEditTextNoteContent.text.replace(selectionStart, selectionEnd, "\"" + selectedText + "\"")
else -> formattedString.setSpan(typefaceCode, 0, selectedText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
if(!isQuote)
mEditTextNoteContent.text.replace(selectionStart, selectionEnd, formattedString)
}
Initially, I had the following formatting Then, I selected the entire text and applied Bold. It should have shown everything bold along with the Strike Through from the previous formatting, but it does not happen. Here is what is happening :
The problem was using the replace() on EditText. Instead of using the replace(), you must use the editText.setSpan() method.
Therefore, the updated Code looks like :
This will not overwrite the previously used Spans