I need to design an editor for my project that allow user to style some part of the text.
I have used an EditText
and bold selected text by adding following codes in onClickListener
of a button.
String selectedText = "<b>" + mEditTextContent.getText().toString().substring(
mEditTextContent.getSelectionStart(),
mEditTextContent.getSelectionEnd()
) + "</b>";
mEditTextContent.getText().replace(
mEditTextContent.getSelectionStart(),
mEditTextContent.getSelectionEnd(),
Html.fromHtml(selectedText)
);
But i am wonder how i can toggle bold style? For example if selected text is already bolded, unbold it, and if not, bold it.
I write these codes to toggle bold & italic styles of selected text.
Create a class and name it
SpanStyleHelper
:Here is the code to use
SpanStyleHelper
inOnClickListener
: