Dynamically Sizing the height of ListView components - android

for practice I am writing an activity that takes a string from a local DB and puts it into a listView. However the string's are of different length, ranging from a couple of characters, to a couple of sentences. Write now I can take them and place them in a listView but, not all of the strings fit. I was curious if/how you can dynamically choose the height of each part of the list view so that the entire string can fit in it.

Just make sure your list item layout is using wrap_content for its layout_height attribute. If you're using a fixed height, it won't scale to fit larger strings. If you want the lists a specific size except for in situations as mentioned, you can also add a android:minHeight attribute as well with the dimension you want as standard.
Also, make sure your TextView is set to multiline (attribute android:singleLine="false").

Related

Android: an easy way to alternate UI elements in one place

There is a dialogue, in one place of which I need to show either one element or another, depending on the situation. Example:
I would like to do this so that the elements below do not move. I want to keep the area occupied by alternating elements of a constant size.
What is the easiest way to do this?
I can, of course, manually change the visibility. Вut when switching, if there is a different height, then the underlying elements will jump. I can manually set their height equal, but this is inconvenient. It will be necessary to correct the heights of all alternating elements every time after I change one of them.
For example, Qt has Stack Layout that allows you to alternate elements and takes the size of the largest of them. Does Android have something like this?
You might be able to use the ViewSwitcher to hold the two layouts.
It holds 2 different child views and measures its height to the biggest child by default.
Here's the documentation for it: https://developer.android.com/reference/android/widget/ViewSwitcher
Just an idea if you can't find something like Stack Layout. I haven't tried it.
You can put all the elements in an horizontal LinearLayout with MATCH_PARENT width for the visible one and 0 for the invisible ones, but keeping all of them VISIBLE. It should always have the largest height and only the MATCH_PARENT width element should actually be visible.

Need advice on view layout for Google Glass app - a table with varying # rows which should fill the screen (dynamic text size)

I'm creating a Google Glass app and need to display a simple table. The table will have 2 columns and a varying # of rows.
I'd like the text size to be dynamic based on how many rows are in use. So if you have just 3 rows and the strings in the cells are short (in terms of length) the text size should be larger. If, on the other hand the view updates and now there are 6 rows and/or the string length in the cells is greater, the text size should be reduced.
Plainly put, the text size should be computed so that the text is as large as possible while still fitting the entire table on screen.
Any advice on how to create a layout to achieve this? I found the GridLayout but I think I'll need to dnyamically update it since the # rows can vary. Then there's the text size issue.
This is the kind of problem that is easy on the Web but hard on Android. So one approach might be to put a webview into your app and show an HTML table you generate locally and inject into the webview.
You might have reasons this won't work for you. If you need a more native approach this is a problem that has been solved in native Android apps, basically by measuring the font size to see if it will fit in a space iteratively. You start small and increase the size until it doesn't fit, then use the size before the one that didn't fit.
Here is a thread about that approach:
How to adjust text font size to fit textview
That will adress the difficult issue of text size.
Once that is solved the layouts are easy, many containers will work including GridLayout, TableLayout or even a series of nested LinearLayouts.

eclipse android gui designer - limit number of items in list

When I drag a gridview into the graphical layout screen in Android Eclipse, and give it wrap content, then it fills the height of the screen with 24 placeholders.
How can I limit its length? Note this is fine when I actually run the application because my adapter adds 6 items and it isn't very long. I simple want a way of telling eclipse that in an absence of real data that it should draw something with say 6 placeholder items
I don't think it's possible to set the number of items just for the aesthetic purposes of the graphical view in xml builder. If you must do it, you could set the height to look acceptable in the XML via a literal value such as 100dp, then set the layout params for height to Wrap Content when you assign and initialize it in your onCreate(). That way, it will look the height you require in the graphical interface editor, but will wrap the number of elements you add to it when it's running on the device.
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
I agree this is not the most elegant solution, but it would probably work.

Layout alignment in List Activity

I'm creating a list view in Android where each row is shown with LinearLayout. The problem that I have is that since the text is different in each line, it isn't aligned in the same way. See for e.g. the "Date" - in different position on each line. What is the best way to create such a view?
Thanks in advance,
Noam
This is the same problem why browsers are slow with showing tables with variable width: you have to calculate the widths after you've parsed all content.
The quickest way is to manually assign a size (width) to your columns.
If you really can't do that because your content is very unpredictable (are you sure?) you could try and find out what the sizes should be from all your content, and then award the sizes.
Have a couple LinearLayouts in your row layout. Set their orientation to vertical and their layout_width to fill_parent. Also set their layout_weight to 1. These will act as columns and when you put stuff inside and for example center it it will always be in the same place.

Calculating Android TextView's text capacity

I have TextView with height and width as fill parent. Is it possible to find out how many characters can this layout hold?
Do you mean how many characters can be entered into the textview and still be fully visible without scrolling? For proportional fonts, that will depend on the specific characters typed, including where the line break opportunities are. I don't think there's a simple way to compute that.

Categories

Resources