Search code examples
javaandroidstringtextviewtextcolor

Change colour of text in only one line of String Android


I'm currently developing an email application and when you go into the view screen it shows you the message at the top and if it is a series of emails, then underneath it will have:

------ Original Message ------

& the previous message below it.

Currently, all of this text is in white but I want to show all text in white apart from:

------ Original Message ------

As this is an email application and different providers format the original message string differently (see examples below:)

  • ----Original Message----
  • ---- Original Message ----
  • ------Original Message------
  • ------ Original Message ------

How would I go about having the whole line that contains 'Original Message' (and the dashes (however many dashes there are)) to be blue?

If this was a normal textView, and I knew the text that would be in the String, then I could use:

String startText = "Test - received<br><br>";
String textColor = "<font color='#0475F7'>-------- Original Message --------</font><br><br>";
String endText = "From: Test 124<br>" + "To: Test 125<br>" + "Subject: " + "&lt;" + "confirm" + "&gt;" + "<br>" + "Sent: January 30, 2017 3:40:42 PM GMT+00:00<br><br>" + "Test";

String text = startText + textColor + endText;

messageTextView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

But as this is an email application, I would not be able to determine the text that will appear in the String every time, as each email is going to be different. The whole message (so top of the message, 'Original Message' line & the message beneath are all contained within one String).

I made an attempt with the below:

if (message.contains("Original Message")) {

    int counter = message.split("-", -1).length - 1;
    int sideone = counter / 2;
    int sidetwo = counter / 2;
    Log.d(TAG, "COUNTER: " + counter);

    int startPosition = message.indexOf("-");
    int endPosition = message.lastIndexOf("-");

    Log.d(TAG, "START POS: " + startPosition + " " + "END POS: " + endPosition);

    String requiredString = message.substring(startPosition, endPosition);

    Log.d(TAG, "REQUIRED STRING: " + requiredString);

    messageTextView.setText(message);

  } else {
    messageTextView.setText(message);
  }

Whilst the counter would tell me how many dashes there are, I could not figure out how to use that to get the whole line with the dashes and the 'Original Message' part in the middle. Also using the requiredString method would work (but cut one dash off of the end) and if the user had used a dash somewhere in their email, then it wouldn't work as it would instead try to get the dashes before the 'Original Message' line.

Therefore, I'm looking for a way to do the following:

  • Retrieve the whole line in the String that contains the words 'Original Message' and all of the dashes that come with it
  • Set the colour to blue (#0475F7)
  • Set the textView with the text of the message at the top (in white) + the 'Original Message' line (in blue) followed by the message at the bottom (in white)

Solution

  • The best way to approach this is with HTML/XML tags. Android has classes that are able to parse HTML/XML and add the necessary styling needed. See this link.

    If you don't want to go the HTML route, you can also check out the Span classes.

    Hope this helps.