Znalazłem nieco inne rozwiązanie i mam nieco inny problem 😃
Kod wygląda aktualnie tak:
public void setBold(boolean b) {
Editable text = etContent.getText();
if (b) {
int start = etContent.getSelectionStart();
int end = etContent.getSelectionEnd();
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
text.setSpan(boldStyle, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
etContent.setText(text);
int s = text.length();
etContent.setSelection(s);
}
else{
int cursorPosition = text.length();
etContent.setSelection(cursorPosition);
int end = etContent.getSelectionEnd();
StyleSpan normalStyle = new StyleSpan(Typeface.NORMAL);
text.setSpan(normalStyle, cursorPosition, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
int newCursorPosition = text.length();
etContent.setSelection(newCursorPosition);
}
}
Nie mogę wyłączyć bolda.
Nie wiem z jakiego powodu tak się dzieje.
(boolean b odnosi się to toggleButton'a)
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean b) {
if(buttonView.getId() == R.id.toggleButton) {
setBold(b);
} else if( buttonView.getId() == R.id.toggleButton2) {
setItalic(b);
} else if(buttonView.getId() == R.id.toggleButton4) {
setUnderline(b);
}