I am trying to remove the bold text from the selected text but when I hit the button it unbolds all the text which I bolded previously. It seems that removeSpan.styleSpan[i] not taking care of what part of the text I selected instead it removes the bold text from the whole bolded span string.
i want the selected part to be unbold only as shown

but after hitting the button for remove span it unbolds the whole line like this

here is my code i am using for it under one button
private void boldText(){
int selectionStartb = texto.getSelectionStart();
int selectionEndb = texto.getSelectionEnd();
if (selectionStartb > selectionEndb) {
int temp = selectionEndb;
selectionEndb = selectionStartb;
selectionStartb = temp;
}
if (selectionEndb > selectionStartb) {
Spannable str = texto.getText();
boolean BL = false;
StyleSpan[] styleSpans;
styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);
// If the selected text-part already has BOLD style on it, then
// we need to disable it
for (int i = 0; i < styleSpans.length; i++) {
if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD) {
str.removeSpan(styleSpans[i]);
BL = true;
}
}
// Else we set BOLD style on it
if (!BL) {
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStartb, selectionEndb,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
texto.setSelection(selectionStartb, selectionEndb);
}
}