Android: How to place views in a grid layout - android

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.

Related

Horizontally and vertically scrollable layout in react native

I am trying to configure table grid layout in react native which could be scrollable horizontally and vertically.
By table grid layout I mean the layout similar to html table/tr/td: all views in the same row should have the same height and all views in the same column should have the same width.
The height of the row should be selected based on the height of the biggest view in the row.
The width of the column should be selected based on the width of the biggest view in the column.
I've started with horizontal scrolling.
This is what I've done:
https://snack.expo.io/#grekonstudio/01_initial
While it looks fine for me in the browser, on my device in looks this way:
The biggest column takes as much space as it needs and the rest of the space is shared between other columns. But it is done for each row independently, so columns border floats. Ideally, I want Row 1, Col 1 and Row 2, Col 1 to be the same size as Row 3, Col 1. That does not work as they and in different rows.
Well, obvious idea is to switch to column base layout, right:
https://snack.expo.io/#grekonstudio/04_column_based
Again, in the browser, it looks different. On the device it looks this way:
On the first look, this is exactly what I want! But apparently, it just moves the problem to another dimenstion. Let's make one View higher than the others, add a vertical ScrollView and we are losing row layout now:
https://snack.expo.io/#grekonstudio/05_same_problem
You can see the bigger cell takes all space and the rows are not having the same size anymore :(
Additionally, I tried two more things:
setting the flex: 1 to Scroll's view contentContainerStyle.
https://snack.expo.io/#grekonstudio/02_flex_1
In this case, it does not scroll at all.
Getting device height and width and fixing the size of the scroll view contentContainerStyle
https://snack.expo.io/#grekonstudio/03_fixed_width
In this case 'flex: 1' works as it works without a scroll view: splitting it to equal size boxes, while I want rows and cols to be adjusted by content
Another disadvantage is hardcoding width: 2 * screenWigth as e.g. for small content I might not need 2 screen width, I might need 1.5 or 1.2 only. It would also not work in portrait easlity.
I've also tried using the DataTable component https://callstack.github.io/react-native-paper/data-table.html, but when added to horizontal scroll view it fails to keep table layout the same way as in my first image: cells in the same column have different width.
Does anyone have a solution for the above?

Android nice display of tabular data

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..

Android - GridLayout cell sizes

I am trying to use GridLayout with ImageViews. My problem is that the images I have are all different sizes. I have set the row count and column count, but somehow I need to set a fixed cell size (somehow uniform - as in cell width = total / num of columns, and cell height = total / num of rows). I want to do this so that when I set height and width of imageview to match_parent, the image view doesn't expand beyond the cell size and the layout looks uniform.
I need 5 rows and 3 columns. Can someone suggest how to do this ?
I answered a similar question here. The theory is to use a wrapper layout for your cell and provide the margins that it needs to it. Then use the imageview inside this layout. Thus the outer layout acts as a container for your imageview and it will never go out of the bound of its parent.

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

Setting column width in Table View

I am having a hard time understanding how to change the width of columns when creating a table view.
I know you can set the width in pixels of a column but that is not what I want to do.
Say, for instance, I have 2 columns and want each column to take up half the screen. How do I do this without explicitly setting the pixels, so that my code can work on multiple screen sizes?
With a TableLayout? You can't do that easily. The closest you can get is to use android:stretchColumns="*" on your TableLayout element. That should allocate any extra whitespace evenly between the two columns.
Just set the TableRow layout_width="0dip"
and then set the two column layout_weight="1"
This will help you evenly divide a row.

Categories

Resources