-1

I am appending String to Editbox. here appendToMessageHistory is method. I want to set separate separate color for the Both String Username and messge
please give solution before you vote down and think it is not relevent in android.

 public void appendToMessageHistory(String username, String message) {
            if (username != null && message != null) {
                messageHistoryText.append(username + ":");
                messageHistoryText.append(message + "\n" + "\n");
            }
        }

any suggestion is appreciated.

Rituraj suman
  • 216
  • 1
  • 16
  • 1
    you can use Html.fromHtml() – SweetWisher ツ Dec 12 '14 at 07:46
  • possible duplicate of [Is it possible to change the text color in a string to multiple colors in Java?](http://stackoverflow.com/questions/8405661/is-it-possible-to-change-the-text-color-in-a-string-to-multiple-colors-in-java) – SweetWisher ツ Dec 12 '14 at 07:49

2 Answers2

2
String htmlText = "<font color=#cccccc>" + username+"</font> <font color=#ffeecc>"+ message+"</font>";
messageHistoryText.setText(Html.fromHtml(htmlText));

Try this hope it will help.

public static Spanned fromHtml (String source)

Triode
  • 11,309
  • 2
  • 38
  • 48
1

Try using SpannableString.

SpannableString spannableString = new SpannableString(editText.getText().toString());
int lastIndex = //index to be used
spannableString.setSpan(new ForegroundColorSpan(Color.BLUE),0,lastIndex, 0);
editText.setText(spannableString);

Check this nice Tutorial.

Anonymous
  • 23
  • 8