Android nice display of tabular data - android

I'd like to display a table in an android UI, with dynamic information.
This table has a constant number of rows an columns.
The format of each cell is also constant and is for instance a textview.
The content of the cells is dynamic.
Though, The text in the cells may be more or less long, so that it is displayed on one line or several lines, that is my issue.
I would like to display my table nicely so that all the rows have the same height, of course basing on the height of the 'highest' cell of the table, and so that the whole table is displayed on the screen.
It seems to me after being stuck in my search, that Android has not been designed to do that, and maybe the way I want to display my data is 'old fashioned'.
So I'd like to ask to you, experts, 2 questions:
Is there a workaround to harmonize row height in a table?
Is there a better practice to display nicely my data?

TableLayout
http://developer.android.com/reference/android/widget/TableLayout.html
you can create a TableRow and then add Views to it to create columns entries.
TableRow row = new TableRow ( context );
row.setLayoutParams( new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT ,
TableRow.LayoutParams.WRAP_CONTENT );
row.addView( <some column view >);
table.addView( row );
etc etc...
Hope this helps..

Related

How to match column/view width when using ListViews on Android

I have a collection of models that need to be displayed vertically.
Each models has 4 values like strings, that all have a fixed width except the first values.
Can't figure out how to make the first views/columns wrap_content and equal in width compared to each other.
I tried a TableLayout, but that does not keep the widths in different columns for the rows.
I tried to use a MvxLinearLayout with a custom adapter that picks the largest width and sets all other matching views/columns..
Both solutions didn't work, and ended up with to much hacking.
Anyone idea's how to display Android vertical collections with equal/matching column widths?
edit: Collection I'm using has structure like
List<Person>
where Person has { string Name, int Age, string Gender }
The Collection View I'm looking for:
(where all first views in row must be all same size)

Android: How to place views in a grid layout

I want to create a grid layout that would consist of 35 cells (7 rows and 5 columns) and place 6 image views that have equal width and height in the following (row, column) cell pairs if rows and columns are zero-based:
(1,1), (1,3), (3,1), (3,3), (5,1), (5,3)
Can I create this layout using XML? How do I do that? May anyone help? Thanks.
you can do it. you have to declare a TableLayout with 7 TableRows. Then you can place empty elements where you don't want anything and your images where you want them, always keeping 5 columns in each row.
Hope it helps.

TableLayout not skipping column 0 when setting android:layout_column = "1"

The xml code on this site: http://www.learn-android.com/2010/01/05/android-layout-tutorial/6/
Gives the table layout shown in the image.
http://www.learn-android.com/wp-content/uploads/2010/01/TableLayout.png
But if you remove the first table row, then the First Name textview appears in column 0 even though its android:column_layout is set to "1"
But on this site: http://developer.android.com/reference/android/widget/TableLayout.html it says "If you skip a column number, it will be considered an empty cell in that row."
IF you want a empty cell in the row, do you have to fill it in with something or set its width?
Is there a better way to get things spaced in the middle of the screen like in the table row?
Thanks
So, the empty cell is there, it just has no width. If there is nothing with a nonzero width, it will look like there is no column 0. To get it to take up some space, there needs to be something there, even if its just an empty View with a width set.
If you want things centered, it might make sense to use a RelativeLayout and use either android:layout_gravity="center" or android:layout_gravity="center_horizontal" to align things in the center of the screen.

can grid view have column spanning one or more rows

Is it possible to have a grid view with 2 columns and 3 rows with a condition
cell1 (row1,col1) spans to row2,col2 as well. ie there is no cell (row2,col1) and the cell (row1,col1) occupy its space as well
A GridView does not support spanning. A TableLayout however does. See this older topic: Android - GridView : Specify Column Span

Android - fix TextView width by string

I think the answer to this question is probably so simple, but I'm struggling....
I have a TableLayout with multiple columns. I want the last column to be of a fixed width, but I want to define that width to just be able to hold the widest possible string from my program. i.e. it is always wide enough to contain "THIS STRING" without wrapping, or wasting any space.
I would like to do this as I have these TableLayouts within a ListView, so it looks very poor when the last column is of variable widths.
I have tried obtaining the string width, even going so far as to put it into a TextView, call getTextSize() then setWidth() on all appropriate TextViews. The problem I hit there is that gettextSize() returns pixels, but setWidth uses ScaledPixels.
I'm sure there is a really simple solution. Can anyone help?
Are you using android:width="wrap_content" in your XML layout to define the width of that last column?
Edit: I think I just understood, you have a list view, that holds a table and you want all rows of the list view to have the same length for the last row of the table. Right?
I can only think of one, very unelegant solution right now and it involves going over all strings before building the list view.
The general logic would be as follows:
Im going to suppose you are getting al strings from an array, lets call it data.
Establish a global float variable to represent the longest string you have, lets call it maxLength.
Create a textview (lets call it invisibleText) in your layout that wont be visible, you can do this by setting
android:visibility="gone"
Then:
int size = data.length;
maxLength = 0.0f;
for(int i = 0;i<size;i++){
invisibleText.setText(data[i]);
float thisLength = invisibleText.getTextSize();
if(thisLength>maxLength) maxLength = thisLength;
}
In you list view constructor:
TextView text = (TextView)findViewById(R.id.the_text_view_you_want);
text.setText(data[position]);
text.setWidth(maxLength)
The table columns should use android:width="wrap_content"
I didnt test this code, but it should work, i've done similar stuff before.

Categories

Resources