I am developing a Calculator app and I want to display symbols like + - / * etc on a different color. Im using a TextView as my display.
I was able to do it when the buttons are being pressed with a code like this
coloredOperator = "<font color=#BED505>"+buttonPressed+"</font>";
textView.append(Html.fromHtml(coloredOperator));
However then I implemented a code on text change to order the operations when a new line is created on my textView, which looks something like this:
public void onTextChanged(CharSequence s, int start, int before, int count){
String message = s.toString();
// I have a java class that takes cares of this
int lastPositionOfBreakCharacter = getLastIndexOfRegex(message, "\\-|\\+|\\/|\\*|\\^");
int length = s.length();
int breakPosition = length-lastPositionOfBreakCharacter;
String text_view_text=t.getText().toString();
StringBuffer sb=new StringBuffer(text_view_text);
// So a new line is inserted before the last character +|-|* etc...
sb.insert(breakPosition,"\n");
textView.setText(sb);
}
The problem is that obviously this last functions strips my text view of all Spannables thus loosing style.
Is there any way to parse the text to find for the special characters, add the corresponding Spannables and then use .setText()?
Or do you have any other ideas on how to achieve what I'm after to?
Thanks!!!