I have a situation where I need to swap 2-5 linear layouts.
The linear layouts are fixed in xml and currently they are either visible or gone.
In some cases, I now require the visible layouts to rearrange.
Can anyone suggest some quick work around?
I did it on my own.
I put all the layouts inside another layout.
On runtime, I fetched all the childs --> layouts of this new parent layout.
Had two lists, visible and gone.
With the required logic, I divide these layouts into these lists and arrange them as required.
Remove all the childs from parent layout.
Add back the layouts from the list as required.
Related
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.
I have a parent Linear Layout inside which there are two Linear Layout and a button.Each Linear Layout inside the parent Linear layout has two items in it. Items include a edit text and a spinner.what I want is to swap this two LinearLayout when i click the button.So my question is How do i do that programatically?. I am new to Android development so please help me to solve this.
On your outermost LinearLayout, try calling removeViewAt(0). This should remove the first LinearLayout. Then call addView() passing it the first LinearLayout.
One simple way would be to just make two layouts. Designed such that when you are ready to use the other one, switch layouts and then set that as the current view.
Or you could do so with various animation classes if trying to get fancy with it. If there is an exact visual effect you are trying to receive, perhaps provide a bit more on what you want.
As Karim mentioned, you can use setVisibilty() to View.GONE and the to View.VISIBLE.
But for a smooth swapping you are going to need to learn about Translate Animation.
Here a working example of how to swap two views (e.g. two LinearLayouts):
ViewGroup root = findViewById(R.id.my_root);
// assumption: root has 2 child views only
// swap left and right (or top and bottom)
View leftView = root.getChildAt(0);
root.removeViewAt(0);
root.addView(leftView);
// now the two child views of root are swapped
Hello! I have just started playing with android layouts and i wonder if there is a general way of applying basic layout so that it will adjust itself to multiple screens and automatically to landscape view. For example:
In the picture above, I have added some buttons. Now what i want to learn is which layout or options(like weight,gravity,alignment) to b used so that they remain the same in Every view & on every screen. Some says to use linear layout within linear and then add weight and alignment. They said that by doing this, you have flexibility to remove any button and yet no other button looses its place(unlike in relative layout). Can there be better way that will have same layout on all screens and yet flexible??
You can use multiple linear layouts if you want to create a FORM.
otherwise Absolute layout is also good but not much preferable.
Relative layout needs practice, as you have to set widgets with respect to other.
multiple linear layouts may be useful.
RelativeLayout is very easy to use and if you learn to align the widgets in it, the layout will look the same on every screen BUT it's good for a layout that is very simple (few widgets on layout) or a layout that you know that will never change because changing on RelativeLayout is so hard and the best way is editing the XML not working on DesignView.
LinearLayout is not flexible like RelativeLayout but making change in it is so simple and other widgets will not lose their positions.
After all if you want to design layout for multiple screen size I recommend to use Fragments.
I am new to android.
As far as I know, there are different layouts like the liner layout, the table layout, and some others. I would like to know in which case what layout would be suitable.
I am unable to categorize the exact differences between the layouts.
Also, can someone please tell me what the specific meanings of the attributes like fill_parent, wrap_parent are?
What type of layout to use really depends on how the layout you are trying to build looks. If you have a simple layout of items one after another, a LinearLayout usually is the best choice. It's easy to work with, but the drawback is that you can't really customize the layout very much. All your views will just end up in a horizontal or vertical list. A RelativeLayout gives you a better way of adding Views that are right/left/top/bottom aligned compared to its parent view. And last but not least, the TableLayout is really great if you are building a grid of views.
All layouts have specific uses. Read android.developers.com, fill parent means your view will cover whole screen area, wrap-content covers that much area that is occupied by the layout's child views. Match - parent takes area equal to its parent area.But using match parent is not good.
What is the difference between linear and relative layout?
LINEAR LAYOUT ::
In a linear layout, like the name suggests, all the elements are
displayed in a linear fashion
Either Horizontally or Vertically and this behavior is set in
android:orientation which is an attribute of the node
LinearLayout.
Linear layouts put every child, one after the other, in a line,
either horizontally or vertically.
Click here ---- for --- Android Docs reference for linear layout
RELATIVE LAYOUT::
In a relative layout every element arranges itself relative to other
elements or a parent element.
It is helpful while adding views one next to other etc
With a relative layout you can give each child a LayoutParam that
specifies exactly where is should go, relative to the parent or
relative to other children.
Views are layered on top of each other in relative layout
Click here ---- for ---Android Docs reference for Relative layout
Optimization::Have a look at Optimizing Layout Hierarchies
The Fewer Views, the Better::
The number one goal for your layouts should be using the fewest number of Views possible. The fewer Views you have to work with, the faster your application will run. Excessive nesting of Views further slows down your application.
A RelativeLayout hierarchy will typically use fewer Views and have a flatter tree than a LinearLayout hierarchy. With LinearLayout, you must create a new LinearLayout every time you want to change the orientation of your views – creating additional Views and a more nested hierarchy. As a result, it is recommended that you first use RelativeLayout for any layout that has any complexity. There is a high probability you will reduce the number of Views – and the depth of your View tree – by doing so.
Linear layouts put every child, one after the other, in a line, either horizontally or vertically. With a relative layout you can give each child a LayoutParam that specifies exactly where is should go, relative to the parent or relative to other children.
From Android developer documentation: Common Layout Objects
LinearLayout
LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute.
RelativeLayout
RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID)
difference is simple: in LinearLayout we arrange stuff in linear manner (one after another), and in RelativeLayout we can place stuff anywhere on screen.
=> Linear Layout is arranged as a list.
Rest they are similar in functionality.
One of the characteristic feature of LinearLayout in Android is use of a property called Weight, which app can specify using android:layout_weight.
This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen.
On the other hand, RelativeLayout do not support weight or in other words, RelativeLayout does not pay attention to android:layout_weight. That's a property of LinearLayout.LayoutParams, but not of RelativeLayout.LayoutParams.
Linear Layouts
Linear Layouts are great for aligning views in rows and columns.
They are a good way to divide up one place using layout weights that will expand or shrink views depending on the size of the Display.
Relative Layouts
Relative Layouts are great for positioning elements relative to one another.
For example putting B below A or putting C in the lower left hand corner.Check the Screen shoot
Relative layout also make it easy to overlap views. For Example : view A is overlapping view B. Check the Screen-Shoot
RelativeLayout is more flexible than LinearLayout but if you have proper knowledge about LinearLayout you Can use that too.
For LinearLayout every attribute has a significant position hardcoded by the developer.
For RelativeLayout you can change the position by relating with others attribute.
The following link should explain visually how the layouts work "Visually"
http://www.droiddraw.org/
Add some components to the window and mess with layouts to see what happens this is how I learned what each one does.
In the relative layout ,all the content in the layout page is related to other contents in the example_layout.xml page
In the case of Linear Layout the elements are displayed in the linear format
The difference between linear and relative layout in android is that in linear layout, the "children" can be placed either horizontally or vertically, but, in relative layout, the children can be placed with relative distance from each other. This is the difference between linear and relative layouts.