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.
Related
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.
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 on LinearLayout (I'll call it the parent) with inside an other LinearLayout (the child)
When I listen to a click on the child layout, I'd like to make the clickable animation on the parent and not only on the child.
How can I make that?
Thank you
One way to do it is to add a OnClickListener to the parent layout and then add the attribute android:duplicateParentState="true" to the inner layout. That way, when you press/click on the inner layout, the parent layout will also receive the event and process any animations and if it was being actioned directly.
In Android:
Given a View from an onTouchEvent, how can I remove the View and its parent?
I have a LinearLayout in which I have a View. Clicking on the View creates a sub-layout inside of the LinearLayout. That sub-layout needs its own onTouchListeners so I cannot attach an onTouchListener to the parent LinearLayout because that will always block onTouchListeners to my sub-layout.
Therefore, I need a means to remove the parent layout given its child layout. I can already remove child layouts from their parents.
If you know how to remove child views from a parent, stick with what you know ;)
The idea is to elevate to a position in the view tree where you can remove your parent view as a child. How do you do that? Easy, get the grandparent view of your view and remove your parent view from the grandparent.
((ViewGroup)view.getParent().getParent()).removeView((ViewGroup)view.getParent());
I created a custom layout which can have an arbitrary amount of child views.
I figured out that the child views are not available when I try to access them in the constructor of my layout view.
So what I currently do is to access them in onMeassure, but it seems to be a bad idea, since this gets called several times.
What is the best place in my layout to init child views? I wish there was a method like onChildViewsAttached(). Any ideas?
Child views are attached to the parent once layout pass is finished, i.e layout() of the view group is finished.
You can also register OnGlobalLayoutListener or OnPredrawListeneron the ViewTreeObserver of your custom layout.