How can I treat several views as one in ConstraintLayout - android

I want set visibility of several views programmatically. Is there some way to treat them as one single view(or viewgroup)?

Take a look at a new feature in ConstraintLayout "group". By grouping views in ConstraintLayout using this feature you can set their visibility as one. See this Medium Post.
Group
Groups, like the Guidelines, are widgets with size 0. But Group helps to apply some action to a set of widgets. The most common case, is to control a visibility of a collection of widgets.

Related

How to make evenly spaced Grid Layout with dynamically added square items

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.

Hide a view when a dependent view is hidden in ConstraintLayout

Is it possible in ContraintLayout to hide/show a view when a dependent view is gone/visible?
Obviously it's possible by using CoordinatorLayout with a custom behavior or by using an wrapped layout, however the mentioned approaches involve additional layouts. I would like to see such an approach that doesn't introduce additional layouts
You should be able to group the views together using the new "group" feature of ConstraintLayout 1.1.x. See this posting on Medium.
Groups, like the Guidelines, are widgets with size 0. But Group helps to apply some action to a set of widgets. The most common case, is to control a visibility of a collection of widgets.
You can do this programmatically.
When you set the vivisiblity of a compounant in your code, change the visibility value of the dependent view.

Why use a CardView instead of a RelativeLayout or LinearLayout?

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.

Android: Can we even "design" our own widgets and layouts?

I read this here:
Android provides a number of ready-made views that you can use to
design and organize your layout. "Widgets" are views that provide a
visual (and interactive) elements for the screen, such as a button,
text field, checkbox, or just an image. "Layouts" are views derived
from ViewGroup that provide a unique layout model for its child views,
such as a linear layout, a grid layout, or relative layout. You can
also subclass the View and ViewGroup classes (or existing subclasses)
to create your own widgets and layouts and apply them to your activity
layout.
If I am not wrong, this means people can even design their own widgets and layouts? Does that ever happen? Any examples?
this means people can even design their own widgets and layouts?
Yes.
Does that ever happen?
Yes.
Any examples?
There are hundreds of examples out on the Android Arsenal alone. Just looking at a couple of columns of the recent items, there are the following custom widgets and containers:
material-drawer
Material Design Android Library
StarBar
AndroidProgressLayout
DiscreteSeekBar
Android-SingleInputForm
SunDate Picker

Is there any way to have UI elements slightly overlap (one on top of one another) without using absolute layouts?

Lets say I have a list on screen, which I always want to be usable. I also want a small image or textview to slightly overlap the listview. Is anything this possible without using absolute layout parameters?
I've never seen it in any android app or tutorial, but there are things like this in many iPhone apps. It adds a nice touch.
(Also, I don't have any code to show because I'm not sure where to start)
Relative Layouts also allow things to overlap. Views declared later in the xml will be on top. I believe that aligning view edges and use of margins should allow you to achieve this affect without great difficulty.
You could use RelativeLayout and set for example android:layout_marginTop="-50dip" android:layout_below="#id/my_list".
As well as RelativeLayouts, you can also use FrameLayouts to stack objects. Other than the z-order (last object declared = highest z-order), the child objects don't depend on the positioning of other objects in the group, so you can just set margins or gravity to position them.
So in your instance, just declare a TextView after your ListView, and position it wherever you want. It won't interfere with the ListView's positioning, and it will sit on top.

Categories

Resources