Bold text is lost in edittext (android app)

18 Views Asked by At

Android studio java-app: When typing more than one bold letter, the previous bold letter loses its bold formatting. Can anyone explain why? I've looked at all the similar questions but I can't find the answer.

editText.addTextChangedListener(new TextWatcher() {
    boolean isBoldInProgress = boldIsOn;
    int ii;

    @Override
    public void beforeTextChanged(CharSequence edittextBeforeChange, int startOfChange, int NumberReplaced, int NumberOfInserted) {
        isBoldInProgress = boldIsOn && NumberOfInserted > 0;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable editable) {

        if (isBoldInProgress) {
            ii = editText.getSelectionEnd();

            // Apply bold to character before the cursor
            editable.setSpan(new StyleSpan(Typeface.BOLD), ii - 1, ii, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            isBoldInProgress = false;
        }
    }
});
0

There are 0 best solutions below