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.
Related
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?
I'm looking to create a table with 3 cells (TableRow) each cell should have the dimensions of the size of the full screen (width and height).
Should I use something other than a TableLayout to get the same effect?
Thanks a lot.
Here is an example of the expected result
The standard way of doing this will be by using a RecyclerView along with a Vertical Layout Manager, each and every one of your element will have width and height as match parent.
I'm trying to implement a tabular layout that has a header and a bunch of rows underneath it. I've chosen the GridLayout (android.support.v7.widget.GridLayout) as there's some requirements for some elements to span multiple columns (but those are of no concern to the question).
My header cells each contain a LinearLayout with a bunch of TextViews, they're dynamically filled in code,for the sake of example, have a look at the image below.
The second row should contain the divider which is a simple view, that should span my header columns (3).
The problem is the width of the divider - if I choose MATCH_PARENT, it will push the GridLayout to fill the whole remaining space to the right. The grid needs to wrap the content and center itself horizontally. It seems to me there's a conflict between the grid's layout (WRAP_CONTENT) and the divider's layout (MATCH_PARENT).
How can I fix the width of the divider without hardcoding it?
http://i61.tinypic.com/2415eg5.png
In red, my LinearLayouts (header), green, the GridLayout itself, the thin blue line at the bottom is the divider.
Thanks,
MO
SOLUTION (as provided below):
I had to set the column weight for the divider to 1, without specifying a width (actually setting it to zero). Because of my specific requirement to handle all of these in code, the solution was to manually instantiate the GridLayout.LayoutParams class and use
ColumnSpec = GridLayout.InvokeSpec(row_index, num_spanned_cols, weight)
Hope this helps others in the future.
If you set the width to 0dp then give it android:layout_weight="1"
(you could give it any weight you want) it should fill all the available space and not push your bounds if I understand what you are asking correctly
I need to create a 2 by 3 grid, where all the screen space is divided evenly - and I know that nesting linear layouts with weights is bad practice, so I'm looking for an alternative solution.
I need the space to be divided evenly, regardless of screen size - 3 rows with 2 columns each.
From what I understand, GridView/GridLayout/TableLayout will not automatically fill space or divide it evenly - so is there any alternative that will suit my needs?
I know you can set the Layout Params programmatically - is this better practice than using nested LinearLayouts with weights?
Thanks for your help
EDIT: To be clear, I intend to display six images, two in each row with three rows. I simply want to divide the screen space evenly - that is, have three rows of the same height, and two columns in each row of the same width.
EDIT2: This is what I'm using as of now, though I am still interested if someone has a better recommendation. I'm not sure this is better than using nested layout_weight values
In my XML I've weighted 3 LinearLayout containers to each take up 1/3 of the available screen height using layout_weight. (XML has a LinearLayout as it's root element)
Each container as two images as children, for which I'm setting the width programmatically as follows:
//Get appropiate width
DisplayMetrics metrics = con.getResources().getDisplayMetrics();
int width = metrics.widthPixels / 2;
//Set gif views to correct width - half of screen size
teamViewGif1.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif2.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif3.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif4.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif5.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif6.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
Again, I'm still interested if anyone has a better solution or advice
EDIT3: Changed layout to use relative layout instead, and now setting both the images height and width programmatically.
I have a parent LinearLayout with a fixed size. At onCreate() I load data and add rows to the parent layout dynamically. Anywhere from 1-10 rows are loaded. I want the rows to always fill the parent, for example if I have 2 rows each row should have 50% height of the parent (50% + 50% = 100%). If I have 3 rows each row should use 33% of the parent height etc.
I tried to use an OnGlobalLayoutListener to get the height of the parent and resize the rows dynamically, unfortunately when I run the app the resizing is arbitrary at best. Sometimes the rows get resized, sometimes they get resized after a noticeable delay of about 0.5 seconds.
Is there a better way to achieve this? Maybe already in the XML as for example with layout_weight (given that the rows only get added in code)?
You can do this by setting the weight sum on the parent LinearLayout to the number of rows you have inside of it. If this is a vertical list of items you would then set the height of each row to 0 and set its weight to 1. If this is a horizontal list of items you do the same but set the width to 0 and set the weight to 1.