Dynamically set the number of textview in a given space - android

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.

Related

Contact list design suggestion

I'm constructing a custom contact list with 5 required fields. It's a bit difficult to fit all 5 in a single row without looking like it's cluttering or doing nested rows which make it hard to distinguish the rows. Here are a couple of designs that I came up with (below). Is there a better way to organize the fields, such as expanding the row when selected to reveal more info, or simply make the row height bigger? Any suggestions?
Not so clear what your five fields are. Most important field(s) (e.g. name or contact number) should be at the top in bold style. The second row should contain less important fields, but you need to put enough spaces and style to distinguish them. You should also use ellipsize(...) attribute to contain them within the boundary. If it is important to show the whole string, you may even use third row for any field. Hope this helps.

Multiple images in one TextView?

I would like to know what is the best way to place multiple, small(all of them the same size) images into one TextView? From what I've found, the best way would be to use Html, but how? All of my images are offline ones, so I can copy them for example in the raw folder, if that is the right way. Can anyone point me into the right direction, or show a similar thread, which I did not find? OR, is there any better approach, like don't use TextView, but something else, which can be solved in the layout files, and dynamically filled with images?
Btw, the whole thing I want to do is:
I have a ListView, filled with items
each item has different attributes, which I currently print in plaintext(I want to replace theese with images
atm, I use one separate TextView to display theese attributes
the number of attributes are random, but at least 1, and typically 3-4(so 1 picture at least, 3-4 typically)
cheers
Sounds like you are making smileys in a chat/message application, am I right? ;)
Anyway, the way to go is to use an ImageSpan. You can use a Matcher to find all text combinations you want to replace, and use a SpannableStringBuilder to add ImageSpans to the positions returned by the matcher, this will replace those characters with the image defined by the ImageSpan.
Why not just create a layout that can be used for each row of your ListView and populate the different elements of that layout based on the data for the row?
For example, if each item has a maximum of 4 attributes, add four ImageViews to the layout, and set their drawables and visibility in getView based on the position passed in.
One thing is for sure : You do not want to put your images inside your textview.
A textview can contain a background but should not be used to contain images.
What you want to do is simply design an item layout that will be used by your adapter to fill the listview.
This item layout will contain a textView that contains only your text and your images. Then in your listAdapter you'll simply show or hide the images you want.
Try to base your layout on a RelativeLayout that will allow you to have a simpler design and even overlap some elements(the images could overlap the textview for example)

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

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

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

Image in a custom List Android

I have created a custom adapter to display a list, there is an image that is displayed in each row ( the image is the same for all rows, except using an array i am assigning it different values depending on the position). An xml file defines the relative layout that i am using. My problem is that i can either get the entire row to be clickable or nothing at all, I only want this image to be clickable, instead of the entire row. How would i be able to do this ? i am new to android and am pretty much following different tutorials trying to create my list. Any help would be appreciated.
layout is like this :
TEXT:
[Image]
TEXT:
thats wat a row looks like...getting two texts from two different arrays and shows it, a third array is used to link to the image. I just want this image to be clickable instead of the entire row.
Thanks
Android's list component manages clicks per row. This makes it very difficult to achieve what you want to do. Two solutions come into mind:
1) If your list is never very long you could simply use linear layout and scroll view to build the list. This approach won't work if you fill in the list dynamically and you can't be sure that there won't be a very large number of rows as it would use too much memory in that case.
2) Other option is to use ListView but make your text components and images different view types in list ie. break you row into three.
That can be achieved overriding list adapter's getItemViewType(int)
http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)
In this approach you can make the image rows clickable but the text rows not.

Categories

Resources