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

Android TextView

how to make textView Show lots of text , thats is to say text which contain more than 5000 characters in andriod, anyone with a solution to this should please help me out

14 Answers

Harry James
Harry James
14,780 Points

Hey krs!

I've never actually encountered this myself but, I did a bit of research on the Android Developer Guide for TextView's and found this:


android:maxLength

Set an input filter to constrain the text length to the specified number.

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol maxLength.


So, if you go into your XML Editor and add this android:maxLength attribute, you should hopefully be able to set that to anything, like this:

<TextView
android:id="@+id/exampleTextView"
android:maxlength="10000" />

Let me know how it goes!

tried that before it didnt work......... its giving me headache

Harry James
Harry James
14,780 Points

Does enabling scrolling do anything?

niether does it..... i have tried alot of stuffs to no avail yet

Harry James
Harry James
14,780 Points

Hmm... I'll look into this and see if I can get anything working.

Harry James
Harry James
14,780 Points

Hey again! Sorry for the wait.

I tried putting a whole story in a TextView (29948 words, 160002 characters) and I had no problems doing so.

Can I ask, what is preventing you from doing so? Are you getting an error message or is the text simply not fitting on the screen?

can you pass me your code to view , because my text is sent over http services and when it pulls in it cuts off some of the words..... i can pass you my xml code if you wana see

no problem you give it a shot

can you pass me your code to view , because my text is sent over http services and when it pulls in it cuts off some of the words..... i can pass you my xml code if you wana see

Harry James
Harry James
14,780 Points

Sure thing! Here's the TextView xml code:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Really long story..."
        android:id="@+id/textView"
        android:maxLines = "999999999"
        android:scrollbars = "vertical"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

And the Java MainActivity code:

TextView textView = (TextView) findViewById(R.id.textView);

textView.setMovementMethod(new ScrollingMovementMethod());

Let me know how it goes! :)

still didnt work for me

Harry James
Harry James
14,780 Points

Hey krs, sorry about the delay - I was spending some time trying out the new Weather App course.

Can you try running this line of code once you get the text from the internet?

Log.d("TEST TAG", returnedString);

Replace returnedString with the string that it returned from your http response

Then, run your app and check the log console in the Debug View. Does the log show the full string or is it still being cut off?

when i loged my app during development it shows full string....all i had to do was use a break point in my return string and debuged.....saw the complete string in debug bug in-app it dont show complete string

Harry James
Harry James
14,780 Points

I eventually managed to reproduce your problem!

It seems as though there's a limit to how many characters there can be in a Java/Gradle String but, not in XML text (There probably is one here but, it'll be a very high number in comparison to the Java String which is why my story didn't reach this). I then did a quick Google search on my error and found this StackOverflow post of someone having the same issue.

Now, there's no simple way to solve this. I actually seems to require a file to get the text from! You could always use the textView.append() method to append multiple strings on if that helps (It takes in a CharSequence which is very interchangeable with Strings). You could also "stream" the String - as shown here but, again, it's not easy.

Hopefully you'll be able to work something out that will work with your app and, I wish you the best of luck on doing it! Sorry that there was no simple answer to this though... :'(

no problem....i just that am still looking for a solution to it.....

Harry James
Harry James
14,780 Points

Yes, I understand where you're coming from.

Android doesn't have an easy method for dealing with large Strings right now. I've looked through pages and pages of Google searches and nothing :'(

i hope some treehouse teacher helps out on this issue

i have spent alot of time on searching for a way out but none yet

Harry James
Harry James
14,780 Points

Hey again krs!

Another user just had the same issue when I was browsing the forums and an idea soon popped into my head and, it works!

I noticed that it works in XML format so, you can simply write your String in Strings.xml in your values directory and, hey presto! You can then use

TextView textView = (TextView) findViewById(R.id.textView);
textView.setMovementMethod(new ScrollingMovementMethod());

String longText = (String) getText(R.string.longText);

textView.setText(longText);

in your Java code and you're all sorted! And, yes, it really is that simple (I couldn't believe it myself).

Hope it helps and, if you need any help with this, give me a shout! :)

Hey Harry,

do you mean i should return my web service in XML and not Json?

Hey Harry,

do you mean i should return my web service in XML and not Json?

Harry James
Harry James
14,780 Points

Ah. I forgot you were getting your String from a website.

I don't believe it's possible to retrieve data in XML (Correct me if I'm wrong though as, I'm not an expert in the language!)

I'm not actually sure how you would go about moving a Java string to an XML String, especially not if it causes an error when the String is too long...

It does seem like the StringBuilder is the best way to go about this if you can understand what's going on.