1

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)
Toby Clench
  • 403
  • 1
  • 5
  • 22
  • I didn't understand what you mean after reading your question twice but as far as i understand why don't you use different textview for dashes and message header – Moien.Dev Feb 01 '17 at 14:54
  • @MCZ In simple terms, I am looking to change the colour of one line of text within a string. The whole message is part of one string which is retrieved as an intent when clicking onto the view screen: `String message = intent.getStringExtra("message");` I could use two/three separate textViews but I would still need to extract the whole line from the 'String message' which contains '------ Original Message ------'. So I would then be looking to extract the content before and after that particular line to form the whole message whilst also retrieving the whole line (and all dashes). – Toby Clench Feb 01 '17 at 15:17

2 Answers2

1

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.

Community
  • 1
  • 1
tonyo.dev
  • 692
  • 1
  • 5
  • 12
0

Managed to resolve this by using Spannable (as suggested above) and then checking the configuration.

if (message != null) {
            if (message.contains("-------- Original Message --------")) {
                Log.d(TAG, "Encromail configuration");
                String wordToFind = "-------- Original Message --------";
                Pattern word = Pattern.compile(wordToFind);
                Matcher match = word.matcher(message);

                Spannable wordToSpan = new SpannableString(message);

                while (match.find()) {
                    Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
                    wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }

                messageTextView.setText(wordToSpan);

            } else if (message.contains("------ Original Message ------")) {
                Log.d(TAG, "App to app configuration");
                String wordToFind = "------ Original Message ------";
                Pattern word = Pattern.compile(wordToFind);
                Matcher match = word.matcher(message);

                Spannable wordToSpan = new SpannableString(message);

                while (match.find()) {
                    Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
                    wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }

                messageTextView.setText(wordToSpan);
            } else if (message.contains("------Original Message------")) {
                Log.d(TAG, "BlackBerry 7 configuration");
                String wordToFind = "------Original Message------";
                Pattern word = Pattern.compile(wordToFind);
                Matcher match = word.matcher(message);

                Spannable wordToSpan = new SpannableString(message);

                while (match.find()) {
                    Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
                    wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }

                messageTextView.setText(wordToSpan);

            } else {
                Log.d(TAG, "Message doesn't contain Original Message string");
                messageTextView.setText(message);
            }
        }
Toby Clench
  • 403
  • 1
  • 5
  • 22