Home » Android » android – TextView – how to make second line indented

android – TextView – how to make second line indented

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to have the second line (and third, fourth, etc.) of my TextView indented, is that possible? I don’t know the exact TextView’s text in advance. The text will be without newline (“\n”) characters – it will be wrapped to lines automatically by the TextView.

How to&Answers:

Another possibility would be to switch from a TextView to a WebView then using CSS set the padding-left of your text to 20px and text-indent to -20px. That should move all text in 20px except the first line which will be pulled back 20px.

Answer:

public static void IndentEffect(WebView webViewv, String string) {
    webViewv.loadData("<html><p style=\"padding-left: 40px;text-indent: -20px;\">" + string.toUpperCase() + "</p></html>", "text/html", "utf-8");
    WebSettings webSettings = webViewv.getSettings();
    webSettings.setTextZoom(webSettings.getTextZoom() + 20);
}

For the above wrapper method, if you just pass the webview reference and the place holder text, then the required indentation will be automatically applied for the second line.

ALL THE BEST !! Might be helpful for someone in need.

Answer:

I feel like you would need to use multiple TextViews. One with a full width and then another one right below it with some padding or margin on the left to give the appearance of an indentation.

In your code you’d have to parse the text so that only a certain number of characters go into the top TextView, and the rest go below.

Not the greatest solution, but it might be worth playing with. If you’re going to do it this way you would definitely want to use a mono spaced font. That way you would always have the same number of characters that could fit on a single line in the first TextView.