GridLayout vs TableLayout - android

I'm unsure as to what the differences between the two of them are and which I should use for my purpouses?
What I'm trying to do is create a custom virtual numpad with text inputs and that can dynamically change its contents to have a date picker.
So I need a layout system which supports many different sized cells inside it.
Which better suits my needs and what's the difference?

In my experience I had both GridLayout and TableLayout give me the same results. They both seem to stretch the columns to fit the widest child element. Neither will give you gird lines or borders around the cells.
From the official docs on GridLayout:
Row and Column Specs
Children occupy one or more contiguous cells, as defined by their rowSpec and columnSpec layout parameters. Each spec defines the set of rows or columns that are to be occupied; and how children should be aligned within the resulting group of cells. Although cells do not normally overlap in a GridLayout, GridLayout does not prevent children being defined to occupy the same cell or group of cells. In this case however, there is no guarantee that children will not themselves overlap after the layout operation completes.
and from the TableLayout:
The table has as many columns as the row with the most cells. A table can leave cells empty. Cells can span columns, as they can in HTML.
So it seems to me that the GridLayout is a bit more versatile and probably what you are looking for.

Related

Android Horizontal RecyclerView with dynamic amount of rows and columns

I need to create a Horizontal RecyclerView with dynamic rows and columns, and every cell has the same width, but different height. Which means column width should be consistent and row height is dynamic.
I've tried implementing this behavior with FlexBoxLayoutManager, StaggeredGridLayoutManager, GridLayoutManager. Also attempted with GridView but then realized it's not supported with horizontal scroll.
I'm trying to find something that is similar to UICollectionView in iOS.
As seen in the photo, columns are unlimited, and number of cells per column depends on the height of the cells. So each column has a different number of cells.
FlexBoxLayoutManager supports what you're trying to achieve.
You want to use it within a RecyclerView have a look at FlexDirection
https://github.com/google/flexbox-layout#flexboxlayoutmanager-within-recyclerview

Image list with text

I want to achieve something similar to the attached image
I was thinking of using TableLayout with 2 columns for each row.
There will be padding in order to be apart of each other. Then I will put white background for the cell. Lastly, I'll just add the ImageView and TextView.
The images and text are dynamically generated. I will get the image URL and display them.
So, are there any better or more efficient way for implementing what I want to achieve? TableLayout doesn't seem to be that efficient.
Your diagram looks pretty much like a grid - for which you can use a GridLayout in Android: https://developer.android.com/reference/android/widget/GridLayout.html
A layout that places its children in a rectangular grid.
The grid is composed of a set of infinitely thin lines that separate the viewing area into cells. Throughout the API, grid lines are referenced by grid indices. A grid with N columns has N + 1 grid indices that run from 0 through N inclusive. Regardless of how GridLayout is configured, grid index 0 is fixed to the leading edge of the container and grid index N is fixed to its trailing edge (after padding is taken into account).
Every one of the items in the grid can be a Cardview (https://developer.android.com/training/material/lists-cards.html) - and that way you will also benefit of a consistent look and feel with Android, without much effort.
Take into account that the cards (every item in the grid) will have the same height, tho: How to make a grid layout of CardViews with variable height?.
If the height of the elements will be variable, you should better take a look to the StaggeredGridLayoutManager: https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html
What you have to do is use a RecyclerView with a GridLayoutManager.
For a full working implementation: https://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/

Dynamically aligned Variable Sized cells in a Layout

What I have to do is draw variable size cells from left to right. Since the data is from server, its length can't be determined (thus the need for variable sized cells.). I have tried the gridviews, tablelayouts but they align them in an order. What I want is something like this:
a sample http://img18.imageshack.us/img18/4136/samplejy.png
the most important thing is they are kind of aligned in a column (though one column can have further multiple columns) but there is no row as such..the cells are in some kind of array which I have to align from left to right and not from top to bottom.. If anyone has tried anything like this it will be very helpful.. thanks

Android TableLayout Format

So I have a table layout that Im adding rows to through code since its not a constant table or a constant amount of rows. The problem Im having is right now my last column isnt fitting all the text because the middle row is set to stretch and its taking up most the view. Is there a way to set weights for the columns so that I can have the one row be bigger than the rest but still show everything else.
The problem Im having is right now my last column isnt fitting all the text because the middle row is set to stretch and its taking up most the view.
Then either change it so the last column is the stretch column, or list both (e.g., android:stretchColumns="1,2").
Is there a way to set weights for the columns so that I can have the one row be bigger than the rest but still show everything else.
By listing more than one in android:stretchColumns, the stretching effect is applied equally to all. You can think of this as the columns having equal weight.

Relative stacking with Linear Layout in Android?

I have 6 images I want to display as 2 rows with 3 images in each. I'm using nested LinearLayouts to achieve this, and it works well except for one thing:
The height of the largest image dictates the size of the linear layout, meaning there is empty space a lot of the time. In other words, my problem is as follows:
I keep getting the layout shown on the left, and I want the layout shown on the right.
I am aware that you can just use GridView, but that will still prevent the exact layout shown on the right, so I'm at a loss really. Many thanks.
Instead of 2 rows of three columns, you need 3 columns of 2 rows. LinearLayouts would be fine, just to be sure set the Gravity of the individual cells to Gravity.TOP.
You could equally achieve the whole grid using RelativeLayout instead of Linear. Each of your bottom row would just need android:layout_below and android:layout_alignLeft set to be the ImageView above it.

Categories

Resources