How to get around the text limitations of a TextView element? - android

Apparently there seems to be a text length limit on the TextView element when used within a ListView. I have to admit I have not checked the actual limit that I am facing, but it is clear to me that 2k characters is no biggie, where 4k characters is simply not being displayed.
And "not displayed" is not even right, as the screen seems to be scrollable for the full length of the TextView (although all I get to see is whitespace).
My issue: I am displaying a ListView for an email message. All parts of the mail are in seperate TextView elements that get dropped into the ListView.
When a lengthy message (somewhere above 2k characters) is put into a TextView, the TextView remains empty but does occupy space.
My question: What alternatives/options are there to make this problem go away?
Eg Making the entire message displayable (even long ones).
Thanks to anyone in advance for assistance.

Is there any reason you have to use a ListView? It seems like you'd be better served with a LinearLayout or RelativeLayout inside a ScrollView, or maybe just put the body of the email inside a ScrollView if you want to keep the other TextViews on the screen.
If you want to stick with the ListView, you might consider using String#substring(int beginIndex, int endIndex) to ellipsize the body and show the full body in another Activity launched in the onListItemClick handler for the list view. You can use the android:ellipsize property in your XML layout (or TextView#setEllipsize in Java code) to limit the text to a single line, but that is probably not what you want.

you can set the singleLine property to true so that only the text visible in single line can be shown on listview or in addition set the maxline property to 1

Have you tried to set android:maxLength property for TextView element in xml
or
to set filter new InputFilter.LengthFilter(maxlength)?

I have found the reason for this quirk!
This is a BUG that exists in Android < 2.2 !
After dziobas pointed out that he filled 10k char strings on 2.2, I tried this out on a new emulator image.
I am still going to change my code in the end to not use the ListView and reimplement my own ListView like class as Brian suggested

Related

Dynamically set the number of textview in a given space

In my app I want to show tags to the users. As of now am showing only 5 tags.
P.S. Ignore the color part.
As of now am using 5 different TextView with wrap content to show the text.
But as you can see, in the latter case, there are lot's of empty space in the top row.
Is there any way by which I can dynamically select the number of tags to be fetched from server which will fit in the given space?
Like in the 2nd case, I can show 3 tags in the first row and 3 in the second row. I'll like to fix a Tab space between two tags and based on available spcae, the number of tags should be displayed.
Given the layout you posted you could use a TableLayout setting android:stretchColumns="*", this will stretch all columns and make it "tabular"
Try using FlowLayout. It works the same way as you want.

How dynamically add/delete textViews to/from linearLayout?

I have db with name of countries(for example).
My first goal is dynamically add textView for (each country) to linearLayout (android:layout_width="match_parent" android:layout_height="match_parent"). In fact adding is not a problem, but problem if the orientation of layout is horizontal and there is to much countries, but the width of layout is not enough to show all of them and they are hide!
So i need to add textViews dynamically, but if there is not enough space for new textView - add it in the next line or create new linearLayout.
I need to create new textView for each country cos than i want to make clickListener for each of them, and there is second goal...
the second goal is delete some of that textView by clicking on it, and the other textView must relocate to that empty space.
Hope that my explanation is clear :) if not i will try another one.
so, i have idea how to add textView: every time when i add new textView - count the length of all previously added ones with this one and compare it with length of linearLayout, if the length of all textViews is less - add to this linLayout, else - create new lin layout and add textView there.
I think this could work, it it looks ugly :)
I hope there are must be more simply and pretty solution!
Talking about dynamically deleting textViews from layout - I have no idea how to do this correctly.
So I will be glad any solutions and ideas, thanks!
EDIT
here is example how I want it looks like in the end:
LinearLayout is usually used for fixed childs.
For dynamic and large number of childs, you should use ListView or RecyclerView, which is exactly designed for what you described.
More than that, ListView and RecyclerView can reuse the child views, i.e. TextView for your case, which is needed, because view objects are heavy to create and keep in memory.
Edit:
Given your image and replies in comment. I would suggest you to use TextView with ClickableSpan, and set update the whole Text on click.
You can check ClickableSpan in the link below.
http://www.programcreek.com/java-api-examples/index.php?api=android.text.style.ClickableSpan
You can use flowlayout for that. Link for flowlayout is https://github.com/ApmeM/android-flowlayout. Just inflate your views inside flowlayout. You don't have to do any calculation when inflating. If there is space available for textviews then they will be added horizontally else in the next line.

Android TextView, move a specified character in the textview to a specified position?

Let's say, I have a TextView with Text "Hello world!". By whatever means, It is shown as
Hel
lo
wor
ld!
Now I want to move the content of the TextView so that the 'w' character is at somewhere like (0, 10) in the TextView. Is it possible to achieve this without overriding the text processing logic of TextView?
I want to retain the position of the text when the size of the TextView changed.
You'd have to override the text processing and the measuring and drawing. Seems pretty daunting. Maybe if you tell us what you're trying to achieve we can suggest another way of doing it.
If you don't mind whitespace you could insert a number of spaces before inserting your character to push the character to the right position.

android custom listview row formatting question

I have a listview that I am trying to customize. The issue I am having with eclipse's android plugin is I am not quite sure how to format the text portions and wrap them around my icon. Here is a diagram with what I am trying to do:
I need to know,
1. Advice on formatting text to fit the row. for the top, i want 3 different strings to fit (what they can) into their own column and ellipsise at the end. easy enough.
2. I want to do something similar for the bottom row but give it a max length. Say each column can contain up to 20 characters, including the (...) ellipses. it may contain 1-7 columns. So I want the overflow to go to the next row and wrap under the icon.
3. Perhaps someone has a war story with a custom listview they want to share? How did you fit all the information in the item, or did you use an alternate view such as GridView instead?
Thank you.
For point 1: You can use LinearLayouts for each row, using the weight property on each element according to your needs.
For point 3: in order to make the text to ellipsise you can use the property
android:singleLine="true"
although it has deprecated, or use
android:inputType
with anything in order to not set the textMultiLine flag into true
For point 2: i cant think of doing this in other way than programatically

android repeater-like functionality

is there any type of 'repeater' type functionality in android? I have a relative layout (inside a row in a listview) and inside that I'd like to have a series of TextViews be displayed one after the other (as if they are child rows in the listview row). The issue is that the number of those "child rows" will vary. is there any way to do this, or should i just create the TextView objects in code, and programatically add them to a linear or table layout?
The closest thing (besides ListView/ListAdapter, naturally) that I can think of offhand is ViewSwitcher and ViewSwitcher.ViewFactory, but there's really nothing magical there: it's an interface that you can call to get a view.
Since it's only one line to get a view and add it to your current hierarchy anyways, though (View.inflate(context, R.layout.somelayout, myContainerViewGroup)) it feels silly to go with something heavier, but if you feel better wrapping that up in a Factory of some sort, check the AOSP source for ViewSwitcher.
One option is TextViews support Multi-line text. So you could create the text with a StringBuilder using "\n" for new lines, and not have to worry about multiple text views.

Categories

Resources