Reputation: 33
I am new to android studio and I am having trouble selecting a layout. There are 5 different layouts. I am using constraint layout because it's the default. Some of my friends said that it's hard to use. What is the main difference between those layouts?
Upvotes: 2
Views: 1242
Reputation: 1968
I've found a very clear answer:
FrameLayout is designed to hold one child object. For example, have Recycler View inside a FrameLayout but nothing else.
CoordinatorLayout is an improved version of FrameLayout. Mostly used for improving the performance of the child object. For example, let’s say we have a ListView and I want to change the scrolling behavior when a user interaction happen. I can use CoordinatorLayout in this to make ListView behave as I want.
LinearLayout is designed to align child objects in one direction i.e horizontally or vertically. For example, If I have a TextView, ImageView, and EditText and I want them to in a line, so I can use LinearLayout with a horizontal orientation to do this.
RelativeLayout is a bit more complex than above two but has a lot more capability. RelativeLayout is designed to align the child object relative to parent or some other child object position. For example, nowadays we usually have a button at the end of the screen on the right side, the ways this is done we create a RelativeLayout as the parent and then a button inside it with parameters alignParentBotton and alignParentRight = True.
ConstraintLayout is an updated version of RelativeLayout i.e providing a lot more parameters than RelativeLayout and improved functionality.
Upvotes: 1
Reputation: 3576
If you've used layout systems in web development like bootstrap or some grid system, you'll find it easy to understand. At first Constraint Layout may look scary and not easy to understand but it really adds a lot of power in positioning the views in responsive manner.
Constraints overview
To define a view's position in ConstraintLayout, you must add at least one horizontal and one vertical constraint for the view. Each constraint represents a connection or alignment to another view, the parent layout, or an invisible guideline. Each constraint defines the view's position along either the vertical or horizontal axis; so each view must have a minimum of one constraint for each axis, but often more are necessary. When you drop a view into the Layout Editor, it stays where you leave it even if it has no constraints. However, this is only to make editing easier; if a view has no constraints when you run your layout on a device, it is drawn at position [0,0] (the top-left corner).
Try this Codelab :
https://codelabs.developers.google.com/codelabs/constraint-layout/index.html?index=..%2F..index#0
Some resources:
https://developer.android.com/training/constraint-layout#constraints-overview
https://developer.android.com/training/constraint-layout
https://www.youtube.com/watch?v=P9Zstbk0lPw
I personally use Coordinator and Relative Layout. You can try them out if you like. I'd suggest that you must begin with these two but definitely try out other layouts in say a example project and learn about them.
https://github.com/codepath/android_guides/wiki/Constructing-View-Layouts
Upvotes: 0