'What is the use of "attach to root" in layout inflater?
I am a newbie to android, I know this question has already been asked but i couldn't get a satisfactory explanation. My doubts are:
- What is "attach to root" used for, if possible with a small and clear example.
- When I searched for the answer people said its optional used to attach to parent view group, What is the view group hierarchy?(Is that considering views that we created programatically or is that also considering the views that are already declared in an xml file)
I hope the questions is clear, if not please say so in the comments.
Solution 1:[1]
The third parameter in the inflate method has a boolean return type.
There is a lot of confusion(Will get to this part soon) when it comes to choosing the value of the parameter.
** It's Simple**
- When attachToRoot = false it means
Don't attach the child view to parent "Right Now", Add it later.
- When attachToRoot = true it means
Attach the childView to parent "Right Now".
In both cases, the child view will be added to parentView eventually.
** It's just a matter of time.**
If you want to read in more details you can refer ---> this answer
(Because I can not post the duplicate answer here, Happy to help).
Solution 2:[2]
For example :
ChildView : TextView
Parent(Container)View : LinearLayout
if attach to root = true
val view = layoutInflater.inflate(R.layout.child, containerView, true)
// Not need -> containerView.addView(view)
// This view will be LinearLayout. Not Textview.
if attach to root = false
val view = layoutInflater.inflate(R.layout.child, containerView, false)
containerView.addView(view) // we should add
// This view will be TextView.
Solution 3:[3]
If you pass a ViewGroup to it, it will add the inflated View to that ViewGroup. That means, the inflated View will be a child of the passed ViewGroup.
It is irrelevant whether the ViewGroup is made programatically or by an xml file.
Solution 4:[4]
I think : Attach to root = true means give the parent view the accessibility wither to make another events when the view is clickable or not.
False: means Not to give the parent any accessibility.
group Hierarchy : it means to be added like a child to the parent "View Group"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Burak Dizlek |
| Solution 3 | Mark Buikema |
| Solution 4 | mohamed hesen |
