I understand the advantage of a FrameLayout when you need to add multiple children to it. But what is the point of adding a FrameLayout as your root element when you only have one child (lets say a TextView)? I mean why wouldn't you simply set that single child (e.g. the TextView) as the root of your layout?
A FrameLayout is - as the name implies - a Layout. It inherits from ViewGroup which is used for positioning and aligning child views.
Of course you would be able to set a TextView alone in your Layout xml. But what are you going to do if you want it to be centered?
Then it would make sense to use a FrameLayout as a sort of "Wrapper" around your TextView which lets you position it the way you want.
Related
I am new to Android Development, so what is meant by child in the below paragraph....
A ScrollView is a FrameLayout, meaning you should place one child in
it containing the entire contents to scroll; this child may itself be
a layout manager with a complex hierarchy of objects. A child that is
often used is a LinearLayout in a vertical orientation, presenting a
vertical array of top-level items that the user can scroll through.
https://developer.android.com/reference/android/widget/ScrollView.html
In android (and most other technologies), views can have subviews, aka "children". Most views can have children and can be a child of a parent view. It's kind of like a "tree".
The most obvious feature of being a child of some other view is that the child moves with the parent view. Another feature is that the child is in the coordinate space of the parent view.
Your paragraph here basically says you can only put one child view in ScrollView and it is usually a LinearLayout. But don't be fooled! This child can have its own child views.
Gaurav i think this is what you looking for, you need to use only one LinearLayout tag inside ScrollView tag, if you are using more than one LinearLayout than it will show error. If you want to use more LinearLayout tag you can use them inside LinearLayout tag which is inside ScrollView tag. May be this will help you.
You need to understand the general concept of child and parent . Simply you can think of the real life relation between parent and child.on the hierarchy parents are toplevel and child is below.so when you come to android layout parent is container and child is the content.
I have a child linearlayout created programmatically and added on a parent linearlayour programmatically.
parentlayout.addView(childlayout)
But, the I want to add again the child layout to another linearlayout, it returns an error, is it possible? What is the best way to do that?
Create another instance of the same class, and set the same properties. I mean Inflate your View again if needed. Because A single View cannot be a child of two ViewGroup parents.
Each view can have only one parent possible.
So you can not add same child to another linear layout.
What you can do is make another instance, apply same properties which you given to previous child. and then add this instance to the parent view.
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
there are many question about "FrameLayout". But i need the exact use of this different from LinearLayout, RelativeLayout. And in a project when we have to use this?
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
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.
Check these links
http://logc.at/2011/10/18/when-to-use-linearlayout-vs-relativelayout/
http://developer.android.com/reference/android/widget/FrameLayout.html
Let me explain the scenario that I want to achieve:-
Consider the below as the Layout I have inside a Parent_Linearlayout:
[Linear Layout] (Fill_Parent, Wrap_Content)
[ScrollView]
Activity's setContentView is set to the Parent_Linearlayout
In the application, when a condition is met, I want the Scrollview to be removed from the screen and instead put another View in its place.
I've been able to do this, & when I remove the ScrollView, I'm applying translate Animation to it so that it seems as if the View has gone to the top -before removing it.
But when the animation occurs, the ScrollView translates OVER the Linear layout present above it.
How do I restrict it, so that the scrollview does not go over the linear layout, but disappears at the base of the Linearlayout. I want the linearlayout to always stay visible..
I've been trying to do this from quite some time, but I've not been able to get desired results..
Could someone kindly help me out here??
I don't quite understand your description of your layout, but the Android view system is drawn based on the ordering of the views in the hierarchy. Views added later to a parent are drawn after those added earlier. So if you always want the LinearLayout to be drawn on top of the ScrollView if/when they overlap, then declare or add the ScrollView object to its parent before the LinearLayout object.
In thinking more about this, I suppose the ordering here is important because you want the ScrollView to be placed below the LinearLayout in the parent of both of these views. Putting the ScrollView first (and thus having it painted first) would then put it above the other LinearLayout, which isn't what you want.
There are various ways to achieve what you want. For example, you could use a RelativeLayout as the parent of the views, then the ordering is not important.
Alternatively, you could place the ScrollView inside another LinearLayout (and that LinearLayout would be the second child of the overall parent layout). Then when you animate the ScrollView, it would be clipped by its immediate parent, which I believe would give you the effect you're looking for (make sure that setClipChildren() is set to true on this new intermediate LinearLayout, which it is by default, otherwise it won't clip the ScrollView as it animates out of it). Note that this approach would necessitate different animation values, since you are now animating the view outside of its parent (the new LinearLayout).