I have a RecyclerView for displaying a list of items. There is this android CardView class given by android to show card layout.
If I use a RelativeLayout and set its background to white it works the same way. Also in case of CardView I have to anyway add a childlayout to it which basically contains the all the views inside the card.
So I wanted to know if their is any benefit of using a CardView (which actually increases the hierarchy of the views) rather than a normal Layout directly.
There are some advantages of cards over layouts, including:
Rounded corners, elevation, etc - visual improvements that come "out of the box" by just using cards
They support various lengths of the content. Actually layouts support that too, but in the context of list/grid views they are meant to have the same size, whereas cards can vary in length (for example when you show comments or descriptions)
Cards on the same hierarchy level can have different types of content/views, unlike layouts (list/grid items) which should have similar layouts when you show a collection
All these and other features can be found in the card's design guideline.
In short:
I'd use cards when I want to display a collection of items which might have different lengths/heights depending on their content (like pictures with descriptions and comments) or a high number of supplemental actions.
I'd use Relative/Linear layouts when I want to display a simple collection of items, all/most of which have the same layout and a limited amount (max 1,2 lines) of text, an icon and an action,etc. elements that are the same for all items
CardView is google's way of styling RecyclerView. You can always use RelativeLayout or any other method prefered.
According to Google's design guidelines, definition of CardView is
Cards are a convenient means of displaying content composed of
different elements. They’re also well-suited for showcasing elements
whose size or supported actions vary, like photos with captions of
variable length.
Using CardView, you get the default Google's look and feel throughout your app. You can customize cardview to your needs. That is why you need to have child layouts inside CardView. Plus Material Design is all about, elevation, colors and animation. CardView can have all these in a relatively easy manner.
It would be nice if you go through Google's design guide lines on CardView.
Related
I'm a flutter user trying out kotlin dev first time, but im kinda confused what the best alternatives for this type of layouts are.
I understand that I use recyclerview for a dynamic number of items. And since theres two dynamic ones (one for number of sets, one for each exercise). Do I use two nested recyclerviews?
It really depends what you're doing here. A RecyclerView is basically a list, so it's a good choice for a scrollable set of items - which it looks like your Exercises are. But how do you want to display those weights?
It's possible to make them scrollable, so some of them aren't visible on the screen (the categories on the Play Store act like this). For that you would need a nested scrolling view of some kind - could be a horizontal RecyclerView, or it could just be a ScrollView wrapped around a LinearLayout you throw views into.
But the other option you might want, is that for each Exercise, all of its weights are visible in a grid of some kind. No scrolling, all there up-front to see and poke at. So the first question is which of those do you want?
I'd assume it's this version - where you can easily visualise the contents - in which case you're not talking about nested lists, it's just one list, and each item contains a grid you can add things too. For that setup, there's GridLayout, TableLayout, and ConstraintLayout Flow (which acts like FlexBox if you're familiar with that). So in the layout for an item in your list, you have a container for stuff, and you put the stuff in it, and the container expands vertically as needed
I would like to create a grid layout in which each item is taking as much space as possible (minus padding), but only as long as there are columns available (after that the next item would be inserted in the next row while keeping the size). Additionally, each item must be a square and is added dynamically.
Example layout with 10 items would be as follows:
I have tried to achieve this by setting weights, ratio constraints, overriding onMeasure - but I just can't get it to work. I would be happy with either a programmatic or an XML-based solution (as long as each item can be added programmatically). I would prefer the solution to be in Kotlin, but I would be happy with a Java-based one as well.
It's probably worth saying that each item in the grid layout is a layout (RelativeLayout as of now) to make inflating it and setting a layered background drawable programmatically easy.
I think you might be able achieve what you want with a different Layout
Have a look at https://github.com/google/flexbox-layout it has lots of methods to control how the cells grown or shrink and includes automatic or manual wrapping of cells.
Take a look at RecyclerView. You would need to pass GridLayoutManager. This tutorial may or may not help you. For square items, I suggest using CardView but it's not necessary. If you are targeting tablets as well as smartphones, check this out. And for dynamically adding new items, you should notify recyclerView's adapter. See this link. You can also extend RecyclerView or GridLayoutManager for more control over items.
I'm trying to make a dashboard like app to display real time data to a user in a vehicle with a layout similar to the included screenshot. I've tried googling for a direction to follow to try and mimic google's responsive grid columns design principle. However, I can't find any examples of that principle in practice on android. How would I go about implementing this type of design in android? Would you use Grid Layouts?
Ideally I'd like to be able to have a Grid Layout that has cells that are consistent in size that allow me to span a Card View across columns and rows but I don't know of a way to do this using a Grid layout. Any ideas?
Screenshot: https://gyazo.com/912c3414d9e8d46a1fa4eade54d620e6
Take a look at GridLayout. It will allow you to define a grid and to span columns/rows. I was also going to mention GridView, but I don't think that it allows spanning of cells.
Another possibility is FleboxLayout if you need more flexibility. TableLayout also permits spanning.
You can also build a grid layout directly with ConstraintLayout using the "GuideLine" object.
Those are four layouts that I would consider.
I have a RecyclerView that display the list of modifications per day for a document. Items are TextView with values like "Line 3 updated to ...", "Line 5 removed", etc. The ViewHolder handle 2 types of views :
Headers : which are the days. These are simple TextView too that are in bold, larger, etc.
Logs: that's what I was talking about : "Line 3 updated ...", etc.
What I would like is that each "day", with its corresponding logs are embedded inside a CardView. But a document can have huge number of modifications per day (>100). So programmatically creating a layout with a CardView as the root, calling 100 times addView() on it to add each logs and then passing this layout to the RecyclerView does not seems a good idea to me.
Is their any way to delimit between a "positionStart" and a "positionEnd" views that will be embedded inside a CardView? It seems to me that this isn't possible or by adding each TextView programmatically inside a CardView but it will then slow down the binding of the views and break the ViewHolder pattern. Am I right or is their a solution I didn't think about ?
You have 3 options to achieve this "grouped in a card" behavior.
(as mentioned) you create the layout yourself and put the whole thing into the recyclerview. This is probably the worst solution since it negates the idea of a recyclerview in the first place.
You just wrap each of your items in a CardView (and set the corner radius to 0dp).
On < 21 devices (I think) there will be some additional padding and every item will appear as its own card, but on higher API versions those cards will lie next to each other and just have some "seam" between them. The shadow on the corner is also a bit buggy, but this is probably the easiest and cheapest solution.
Alternatively you can also create a custom view that fixes the errors mentioned above (margins between and shadow) and use your own to wrap the views. (I believe this is what the Inbox app does if I recall correctly, which also features lists in cards.)
You use an ItemDecoration. For this approach you need a kind of stable setup of your dataset, but if its just the headers and logs, you can draw a shadow above the header, draw borders to the left and right of every item, and draw a shadow beneath the last log. This will also require some setup, and if you introduce further view types you will also have to modify this code (it's highly dependent on your data set)
The 1. method is probably the worst idea. It will work for small lists.
The 2. method can work, but you either will have to create your own custom view or live with a "bugged" version on lower api levels.
The 3. method is something I tried once for fun, and will work, but you will have some additional dependency between your data, your adapter, and your decoration. You can see an example of this decoration here on GitHub. It just draws a shadow around all of the items.
I understand the items that are visible to the user will be loaded when using GridView. Does this also applied when using GridLayout?
I currently working on laying out the applications' icon like the applications screen does. When user pressed the icon of the application they are allowed to arrange the icon in an unoccupied space.
At the moment, I don't know which widgets is preferred to use. If GridLayout behave the same as GridView, I would stick with that so no need to change the code.
If you want to be able to drag icons to rearrange them I would consider using GridLayout with RecyclerView. Take a look at Drag and Swipe with RecyclerView
Part Two: Handles, Grids, and Custom Animations.
If you are unfamiliar with RecyclerView and ItemTouchHelper then also look at Part One first