My app should allow users to style inputted or selected text in an Edittext. Some of these styles are Underline, Strike through, Bold and Italic. They are easily added but I don't know how they could be removed and how I could determine if that style has already been added to the selected text. Code for adding style:
Spannable str = myEditText.getText();
if (!SpannableUtils.isEmpty(str)) {
str.setSpan(new StrikethroughSpan(), startSelection, endSelection,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
I tried using removeSpan but it didn't work. Also tried using
str.setSpan(new StyleSpan(android.graphics.Typeface.DEFAULT),
startSelection, endSelection,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
but it poses the error : The constructor StyleSpan(Typeface) is undefined.
I think this class solve your problem.
How to use:
android.graphics.TYPEFACE.Default
returns aTypeface
, and theStyleSpan
constructor takes anint
as paramater.Try using :
instead of:
I think this way below is the simplest but useful. It works for me.
Please try this code: