Android - Make EditText Normal again after setting Bold / Italic

631 Views Asked by At

I want an EditText that should make the Text that the user will type to Bold / Italic / Underline / Strike-through on the Button Click. I am able to set the Bold / Italic / Underline / Strike-through by using :

 StyleSpan(TypeFace.BOLD)
 StyleSpan(TypeFace.ITALIC)
 StyleSpan(UnderlineSpan())
 StyleSpan(StrikethroughSpan())

respectively. But, when I click the normal button, I want the EditText to make the StyleSpan normal again. I have used the StyleSpan(Typeface.NORMAL) to make it Normal on button click. But its not working.

So, suppose I apply Bold option and then the Italic Option, It should make the text that will be typed to Bold and Italic. This is working as expected : Bold and Italic are setting future text to Bold and Italic

Now, once I unpress the Bold and Italic Button and Press Normal Button again, I expect it to make the text Normal again as I have set StyleSpan(TypeFace.NORMAL) in the on click of Normal button.

But, It is not working as you can see : EditText not becoming normal again

Code :

 private fun initiateFormatting(
    isBold : Boolean = false,
    isItalic : Boolean = false,
    typefaceCode: StyleSpan = StyleSpan(Typeface.BOLD),
    isUnderlineSpan: Boolean = false,
    isStrikeThroughSpan: Boolean = false,
    isNormal : Boolean = false
) {
    val startLength = mEditTextNoteContent.length()
    mEditTextNoteContent.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable) {
            if(startLength < mEditTextNoteContent.length()) {
                when {
                    isUnderlineSpan -> mEditTextNoteContent.text.setSpan(
                        UnderlineSpan(),
                        startLength,
                        mEditTextNoteContent.length(),
                        Spannable.SPAN_EXCLUSIVE_INCLUSIVE
                    )
                    isStrikeThroughSpan -> mEditTextNoteContent.text.setSpan(
                        StrikethroughSpan(),
                        startLength,
                        mEditTextNoteContent.length(),
                        Spannable.SPAN_EXCLUSIVE_INCLUSIVE
                    )
                    isNormal -> removeSpan(startLength)
                    isBold || isItalic -> {
                        mEditTextNoteContent.text.setSpan(
                            typefaceCode,
                            startLength,
                            mEditTextNoteContent.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                    Log.d("Called", "" + typefaceCode.style)
                    }
                }
            }
        }

        override fun beforeTextChanged(
            s: CharSequence, start: Int,
            count: Int, after: Int
        ) {
        }

        override fun onTextChanged(
            s: CharSequence, start: Int,
            before: Int, count: Int
        ) {

        }
    })
}
2

There are 2 best solutions below

7
akashzincle On

try this method, in this you are passing null in first argument to reset previous properties

textView.setTypeface(null, Typeface.NORMAL);
0
maxmitz On

StyleSpan(Typeface.NORMAL) does not work. You can do it like the answer from this question. There all StyleSpans are checked and can be deleted.

Spannable s = bodyText.getText();
StyleSpan[] spans = s.getSpans(sStart, sEnd, StyleSpan.class);
final StyleSpan bold = new StyleSpan(android.graphics.Typeface.BOLD);
if (spans.length == 0) {
    s.setSpan(new StyleSpan(Typeface.BOLD), sStart, sEnd, SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
    for (StyleSpan span : spans) {
       if (span.getStyle() = bold.getStyle()){
           s.remove(span);
       }
    }
}