I am selecting a part of the TextView and on click of a "highlight" button, I am sending the start and the end index of selection to the database. Then I am loading all the start and end indexes from db and changing the color of text between them. 

The problem is after once or twice, the app is changing the color of text that is not in selection.. and the selected part remains unchanged.

MY CODE:

1. When user selects and presses the highlight button

```
int i=contentText.getSelectionStart();
int j=contentText.getSelectionEnd();

db.insertHiglightIndex(String.valueOf(i),String.valueOf(j));
setHighlightedText();

```
2. The setHighlightedText() method..

```
String fullText=contentText.getText().toString();
for(int i=0; i<db.getAllStartIndex().size();i++){
    String a=fullText.substring(Integer.parseInt(db.getAllStartIndex().get(i)),Integer.parseInt(db.getAllEndIndex().get(i)));
    fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
}
contentText.setText(Html.fromHtml(fullText), TextView.BufferType.SPANNABLE);

```

MY SCREENSHOTS.

**The selection:**

![](https://i.stack.imgur.com/8j2yN.png)

**The Result:**

![](https://i.stack.imgur.com/VdCL9.png)

Clearly the selected area is from **"Garrick"** to **"Bart"**, and the result is from **"entity"** to **"2012"**

I am not able to understand why is this happening. I think there is some problem with this `<font color='red'>"+a+"</font>` line.

Thank you