I have been mostly designing my views using Relative layout as it works great when you put things positioned from the right edge, center or simply relative to each other. That would great on different screen sizes so the views are more spread.
With the introduction of constraint layout, I find it that it is more powerfull and it achives the same purposes while giving more flexibility.
Does that mean that Relative layout is no longer needed and can be replaced with Constraint all the time? Is there a situation where you shouldn't go constraint view and go for relative layout instead?
Thank you
It is not much different than other layouts you are probably already using (like RelativeLayout or LinearLayout). The attributes are very similar to the ones used with Relative Layout.
This is primarily designed as a visually oriented tool. The visual editor is intended to be the main way developers interact with their layouts.
The main goal of this new layout, is to help developers create complex layouts that are optimized to rendez quickly. In fact, its aim is to reduce layout hierarchies induced by other layout types.
Related
So I have been writing some Android code for a week or two and some layouts are self-explanatory such as gird, table, and so on. However, I am still confused about when to use Linear layouts, when to use Constraint Layouts and when shall I use Relative Layouts. I know that Google prefers Constraint layouts but, a lot of code samples use other kinds of Layouts. I am confused if I shall even prefer other kinds of layouts or not?
The idea is to get the less nesting in your layout as possible, this for performance, if the layout is quite simple and show a vertical or horizontal layout, I will use LinearLayout, otherwise I will use ConstraintLayout because it's the more powerful layout and any layout can be implemented with ConstraintLayout even the simple ones and it's the evolution of RelativeLayout, also android recommends to use that layout.
I was trying to create different layouts for different screen sizes with swXXXdp layout files. Unfortunately I still can't separate 5,0" from 6,3" as visible on attached image.
Both sizes still use the same file: layout-sw412dp. Any idea for this?
I suggest you yo use Constraint Layout, maybe it could be useful for your goal...
ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.
doc
My boss refuses to let me use RelativeLayout. Everything is done using LinearLayout and minimal use of RelativeLayout, such that the layout that I could run with two levels of nesting with RelativeLayout, I now have to do with four using LinearLayout. Any comments on this? What links can share?
In my opinion it would be better to combine these two elements to get the perfect layout.
It depends on the level of nesting indeed. Android suggests to use a minimum Layouts in Layouts as possible, and if you can achieve with one layout, what you want to do with 3, why not?
The app will run faster for it, though with 2-3 nestings you wont see it.
I for example, create a bunch of included layouts, which i reuse in other layouts.
And the level of nesting could get very deep with that
With the LinearLayout we get the clear separation of view or we can say that each layout is separate. With the Linearlayout each UI element us independent of the other. You get the maximum flexibility. It enables to give the vertical and horizontal orientation.
Linearlayout you can control look more and make it almost similar even
on the device change. And also, using LinearLayout is simpler though
starting phase can be quite confusing
From the Docs
A Layout that arranges its children in a single column or a single
row. The direction of the row can be set by calling setOrientation().
You can also specify gravity, which specifies the alignment of all the
child elements by calling setGravity() or specify that specific
children grow to fill up any remaining space in the layout by setting
the weight member of LinearLayout.LayoutParams.
LinearLayout is a view group that aligns all children in a single
direction, vertically or horizontally. You can specify the layout
direction with the android:orientation attribute.
Again it depends upon your need..I also agree with #Lena Bru answer.
What Is A Relative Layout?
After linear layouts, which display controls in a single row or column, relative layouts are one of the more common types of layouts used by Android user interface designers. Much like other layouts, relative layouts can be defined within XML layout resources or programmatically in the application's Java code. The relative layout works much as its name implies: it organizes controls relative to one another, or to the parent control itself.
Get to love the RelativeLayout. It will make your life much easier, when designing for multiple resolutions/densities. If you have an old SDK, update your eclipse plugin. It has graphical snap-lines for RelativeLayouts similar to designing a form in Visual Studio, so you can see what is anchored where. It's really quite good.
google says:
Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint.
I need to implement an overlay (translucent) screen for my app, something similar to Showcase View
My guess was to use FrameLayout for this usecase, because it is used to stack items on top of each other. But I was surprised to see that the above library uses RelativeLayout.
My question is when to use FrameLayout then, if not in cases like this? What are the disadvantages if I go the FrameLayout way?
A common rule of thumb when choosing layouts is to select the combination that results in the smallest number of nested layout views.
Specific to your question, RelativeLayout is larger and more capable than the much simpler FrameLayout. So for simple layouts, the latter is probably more efficient. But if using RelativeLayout and it's added positioning options allows you to implement your GUI in a smaller number of layout views, then that would likely be a better choice.
Here's a page that discusses some trade-offs and demonstrates some helpful tools to use when designing your layouts. It mostly talks about RelativeLayout and LinearLayout, but is also apropos to your choice between RelativeLayout and Framelayout. Just keep in mind that FrameLayout is an even simpler layout.
Edit (2017): For even more complicated layouts, you may be able to avoid nested layouts by using ConstraintLayout.
I have seen the difference i.e in the latest android SDK versions the default layout given in the main.xml file is Relative Layout.
What is the reason behind that..Is using relative layout recommended to be used more than linear layout?If yes...Please explain..
Thanks In Advance.
To provide us the more flexibility and freedom in programming of UI.
LinearLayout arranges elements side by side either horizontally or vertically(rows vs columns).
RelativeLayout is a layout manager that helps you arrange your UI elements based on some rule. You can specify thisngs like: align this to parents left edge, place this to the left/right of this elements etc.
As mobile apps are going with much more interactive and complex UIs, RelativeLayout helps in building those UI plus reducing the layout hierarchy considerably with so many tags provided.
This improves the performance of the app too.