Home > Mobile >  How do I make my Android app prefix each displayed line of text with ">"?
How do I make my Android app prefix each displayed line of text with ">"?

Time:05-14

My app displays a long line of text like this:

enter image description here

I want to add a ">" character to each line, so I tried the following code:

private String addPrefixChars(String text) {
    StringBuilder builder = new StringBuilder();
    StringTokenizer st = new StringTokenizer(text, NL);
    builder.append(NL);
    for (int i = 0, count = st.countTokens(); i < count; i  ) {
        builder.append(">").append(st.nextToken()).append(NL);
    }
    return builder.toString();
}

and ended up with this:

enter image description here

Note that there's only one ">" character for the entire text, whereas I'm trying to make each displayed line start with ">". Anyone have any ideas how to do this?

CodePudding user response:

I've tried to achieve what you need.

Text :

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. When an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Code to add ">" in each line of TextView :

  TextView content = findViewById(R.id.content);
  content.post(() -> {
            ArrayList<String> lines = new ArrayList<>();
            String textContent = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. When an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
            Layout layout = content.getLayout();
            int lineCount = content.getLineCount();
            for (int i = 0; i < lineCount; i  ) {
                int lineStart = layout.getLineStart(i);
                int lineEnd = layout.getLineEnd(i);
                if (lineStart <= textContent.length() && lineEnd <= textContent.length()) {
                    String lineString = textContent.substring(lineStart, lineEnd);
                    lines.add("> "   lineString   "\n");
                }
            }
            content.setText("");
            for (String line : lines)
                content.append(line);
        });

output :

enter image description here

CodePudding user response:

I ended up changing the field containing the original text to a TextView and creating an EditText for the reply. I didn't have to prefix the original with ">"; instead, I just italicized it to distinguish it from the reply.

  • Related