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.
Related
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
As far as I understand Layouts in Android I just wanted to ask whether the following statement is true or not ?
Is it true that every RelativeLayout in Android can be programmed with a LinearLayout ?
I personally believe that every RelativeLayout can be also done with a LinearLayout. So why should someone use RelativeLayout rather than a Linear ?
Thank you
Is it true that every RelativeLayout in Android can be programmed with a LinearLayout ?
No.
First, RelativeLayout supports Z-axis ordering (i.e., widgets overlapping other widgets). LinearLayout does not.
Second, only a subset of RelativeLayout structures could be replaced by a single LinearLayout. It is conceivable that you could implement all non-overlapping RelativeLayout structures using many LinearLayout and Space widgets. The result may be substantially more complex, more memory-intensive, and possibly more CPU-intensive.
According with android reference guide:
LinearLayout: A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen.
RelativeLayout: Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
For more information: http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
Even if that statement was true, RelativeLayout is simply a better tool for certain use cases - so why don't use it? You might be able to recreate a layout using a LinearLayout, but the result will most likely be inefficient in terms of number of views and rendering times. Additionally, sometimes using RelativeLayout leads to a more simple and intuitive layout description, which means easier maintainability.
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.
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 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.