Issue
My app displays a long line of text like this:
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:
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?
Solution
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 :
Answered By - Mouaad Abdelghafour AITALI
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)