Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How to split or display a long text string in textview to sections or pages. Android

How would i be able to display a long string of text in a textview on android, because i have tried alot of techniques and none seems to work. A case study of what i am talking about is the way the text in the BBC android app is displayed in various sections because the text strings are long.

4 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi

I had this problem recently, the easiest way around i found way to turn the string into an html string and then place it in a scrollable TextView. I can then add in line breaks by using

<br>

tags which make line breaks in the text view where necessary. It would go a little something like this....

String myHtmlFormattedText = "<br>(officially Treehouse Island, Inc.) is an online interactive education platform that offers courses in web, mobile and business development.<br><br>Its courses on web development and programming are aimed at beginners looking to start a new career, while its courses in business education and marketing teach students how to start and market a business in the technology industry."

TextView tv = (TextView) findViewById(R.id.textview1);
tv.setText(Html.fromHTML(myHtmlFormattedText));
tv.setMovementMethod(new ScrollingMovementMethod());

Hope this helps

Daniel

hmmmm...... i am passing my string from a JSON object....do you think this method would still work?

Daniel Hartin
Daniel Hartin
18,106 Points

If you have control over the string then yes however if not the only way I know of would be to do a replace all function replacing the full stops with full stops and line breaks which of course may not be the correct formatting you were looking for. If you are downloading the JSON object from another source I'm afraid the method I have shown won't work. Sorry I can't be of any further help.

ok...... its so anoying that google dont have a good documentation as how to implement this function..... and i dont know how its been implemented on the bbc and ccn apps

Daniel Hartin
Daniel Hartin
18,106 Points

I downloaded the app from the BBC and as they control the original information on their website it is highly likely they have marked up the text beforehand so have implemented something like I have mentioned already, or they have designed an interface that is totally separate to the inbuilt android features. When I looked at the same problem for my app I had control over the input strings so it was easy. I think the only way would to do a replaceAll on string over x amounts of characters in length (or some other logic). HTML has some other tags which define pre formatted text however i doubt these will be off use to you either when returning the information from a third party service.

kinda dont know how to go about it......