I'm writing an app with a custom theme and layout (some CI) for API 18+, so I need to align a lot of elements.
I read several times, that one should avoid nested layouts as good as possible, but also that some layout types are less efficient than others (RelativeLayout).
should I still avoid nested views these days?
if yes, what is more efficient
flat RelativeLayout
nested LinearLayout (2 levels)
what about TableLayout with TableRows
Currently I already have this layout hierarchy:
Drawer (for sidemenu)
Relative (because of header and footer)
Frame (Content goes below here, just used for easy replacing of fragments)
Your current layout hierarchy is very sample and under normal circumstances it should not be slow at all. Of course the FrameLayout is not needed as you are using a relative layout because your can align the center content below and above the header and footer of the relative layout children.
For this level of nesting the view there is absolutely no problem as for performance.
Both RealtiveLayout and LinearLayout extend FrameLayout with relative being a little heavier as it handles what else the relationships between the views.
If you need to align many element (like editexts and textview) in a form-type layout the best option is TableLayout.
General speaking layout inflation from XML is expensive, but nesting a view inside another makes no difference in real life. The problem would be if you had a thousand views in a TableView with TableRows having nested layout inside.
Related
Can one relative layout / linear layout can have multiple constraint layouts? Do they affect on the performance of application while inflating?
Also, can one constraint Layout can have multiple constraint layouts?
If you are talking about nesting view groups(relative layout/linear layout/constraint layout) inside another view group, yes it is possible. But the issue with nesting view groups is each of the view group has to perform calculations to constrain its children. This is usually done in a depth-first search manner. So it will affect the inflation speed and thus the overall performance of the layout.
It is always better to keep the hierarchy as flat as possible. Which means maximum avoid the nesting of view groups wherever possible.
Nested view hierarchies should be avoided because it takes more time in rendering. To overcome from box-model logic (in HTML), ConstraintLayout has been introduced. It is clearly mentioned in the documentation.
I've created a very big layout (approx 2000 lines) that contains a large number of views and sub-layouts, often with weighted measures. The tree is pretty complex and goes down to 8 or more levels of nesting.
To create different "perspectives", the app programmatically sets some of the views' visibility to View.GONE, expecially those one that are at the head of a tree of views that I don't want to be displayed, in different moments of the app life cycle, and sometimes changes the weights to resize views; hence, the final layout that is displayed to the user contains at every time not more than 1/3 of the views and view-trees that are in the main layout.
I don't care about the performance at "switch" time, so when the user switches from one perspective to the other, I've got plenty of time to switch on and off the visibility and that's not a problem.
Instead, does having so many view and view trees set to View.GONE affect performance at runtime? What is the effect of GONE views on CPU and Memory performance, in contrast to a smaller layout (maybe programmatically inflated) where all views are displayed?
Actually while creating complex layout have to use constraint layout.
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.
Please find the doc from url :
https://developer.android.com/training/constraint-layout/index.html
Suggest me on this
I have to use some header and body part in android screen design, Can i use plain Linear layout for screen design or can i use relative layout or else both layout combined together.For the header bar im using a gradient image and application runs both in vertical and horizontal orientation.
As of now im using two main linear layouts for the first one im using a height of 40 dp and for the second i just used 0dip is this a correct way of approach or i have change anything.
Don't mix concept of RelativeLayout and LinearLayout. RelativeLayout is preferred because it reduces extra lines as compared to LinearLayout. In RelativeLayout views are placed relative to each other i.e. left, right, top and bottom unlike LinearLayout where you can't place view in respect of some other view. Both have its own advantages. As Weight concept is not supported by RelativeLayout but LinearLayout.
Depending on the complexity of layout both are chosen. One thing to avoid is un-necessary nesting of layouts which reduces performance. I would recommend read concepts of RelativeLayout, LinearLayout and weight first then you will be able to judge which layout to use on your own. Till then use RelativeLayout as it requires minimum number of lines.
You can use Linear-Linear, Linear-Relative or Relative-Relative. Anything you want.
Your question is hard to understand. From what i get, i think your approach is fine. You should let the Screen design (second layout) use "match_parent". It will take up remaining part. For your header layout using "40dp" is fine. I made app with Header, and i used this approach.
If in Header, you are adding images as well as TextView, it is advisable to use RelativeLayout. In the rest part, use however you need it.
I got two views which are equally distributed on my main layout.But my two views in tern have nested childs which are again equally spaced.I am using nested layout weight's which is working fine. Whether nested weights effects the performance.What is the work around for this?
Every view — or worse, every layout manager — that you add to your
application comes at a cost: initialization, layout and drawing become
slower. The layout pass can be especially expensive when you nest
several LinearLayout that use the weight parameter, which requires the
child to be measured twice.
Refer: http://developer.android.com/resources/articles/layout-tricks-efficiency.html
You can use a RelativeLayout in most cases to avoid such expensive measurements. In a RelativeLayout, views are aligned with their parent, with the RelativeLayout itself, or with other views.
To clearly understand how the views are positioned with respect to each other, a wireframe of the layout can be captured by using HierarchyViewer perspective of Android SDK.
I have read many articles regarding layout, but I am still quitely confused. My questions are:
When to use relative layout? Example?
When to use table layout and why we can't use it instead of relative layout?
When to use linear layout?
I just need brief answers.
When use which layout?
I think It depends on your UI, and most important thing that how you create optimized layout.
From definition : -
LinearLayout – designed to display child View controls in a single row or column. This is a very handy layout method for creating forms.
RelativeLayout – designed to display child View controls in relation to each other. For instance, you can set a control to be positioned “above” or “below” or “to the left of” or “to the right of” another control, referred to by its unique identifier. You can also align child View controls relative to the parent edges.
TableLayout – designed to organize child View controls into rows and columns. Individual View controls are added within each row of the table using a TableRow layout View (which is basically a horizontally oriented LinearLayout) for each row of the table.
References :
Creating Efficient Layouts
Common Layout Objects
And most important Hierarchy Viewer
at first there is some confusion about these layouts but as you start playing with these three layouts u will get idea where to use what.. I worked on relative-layout the most.
Consider i want to use a widget always at bottom of screen then with table or linear layout this is not possible always.. without feeling screen other two can not make item at bottom but relative can do.use of any type of layout depends on your screen requirements.
I started out using relativelayout. But recently I've switched to using mostly linearlayout.
The reason is kind of hard to explain, but take this as an example: Say I want a layout that has two images centered in the middle of the screen. Both images should take up 1/4 of the screen width and 1/4 of the screen height. This is impossible to do with relativelayout assuming you want it to work exactly the same on all devices. But you can do this with Linearlayout. By creating vertical and horizontal parents, you can create "boxes". To accomplish this you must learn about weigthsum and weigth. Parent layouts should have the weigthsum attribute and children should have the weight attribute.
Anyway, my point: Relativelayout is easy to use but it's also deceptive. You may think that your layout will look exactly alike on all device, but most likely, they won't look alike. The reason for this is:
With relativelayout you must define size with either dp or px(assuming you don't fill parent or wrap content).
Different devices have different aspect ratios.
I hoped that helped in terms of understanding relative and linearlayout.