0

I am looking for a way to set the maxLine property of a textview based on the length of my string. For example, i have a string in the following format:

String test = "This is a \n \n test"

The above string would have 4 lines and i would like to assign this to the textView.maxLines property of the textview to make sure the textview will be able to display all the lines present in the string. How can i achieve this?

John Baum
  • 2,865
  • 7
  • 35
  • 78

2 Answers2

0

Try:

textView.setMaxLines(("This is a \n \n test".split("\n").length+1));
rahulserver
  • 7,839
  • 18
  • 75
  • 137
  • One thing that might no take into account is if the string is so long that there are implicit line breaks that the widget will require to wrap the content – John Baum Jul 19 '13 at 14:54
  • @JohnBaum so what you need is if the string is too long you need to break the string into multi line text? – Raghunandan Jul 19 '13 at 14:58
  • @JohnBaum http://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds. may be this will help whatsoever. – Raghunandan Jul 19 '13 at 15:00
  • @JohnBaum if text is too long, then u may make the textView scrollable.See this http://stackoverflow.com/a/3256305/1291122 answer – rahulserver Jul 19 '13 at 15:01
-1

try android:height="wrap_content" in the manifest file

Vadym Kovalenko
  • 689
  • 1
  • 11
  • 27