What is the use of "attach to root" in layout inflater?
Asked Answered
O

5

27

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:

  1. What is "attach to root" used for, if possible with a small and clear example.
  2. 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.

Orotund answered 11/3, 2014 at 12:52 Comment(2)
Check here for a good explanation: doubleencore.com/2013/05/layout-inflation-as-intendedLublin
possible duplicate of What does the LayoutInflater attachToRoot parameter mean?Roulade
B
64

I myself was also confused about what was the real purpose of attachToRoot in inflate method. After a bit of UI study, I finally got the answer:

parent:

in this case is the widget/layout that is surrounding the view objects that you want to inflate using findViewById().

attachToRoot:

attaches the views to their parent (includes them in the parent hierarchy), so any touch event that the views recieve will also be transfered to parent view. Now it's upto the parent whether it wants to entertain those events or ignore them. if set to false, they are not added as direct children of the parent and the parent doesn't recieve any touch events from the views.

Hope this clears the confusion

Blockhouse answered 24/3, 2014 at 16:25 Comment(3)
Thank you for this short and precise explanation!Ferrel
This is better https://mcmap.net/q/101020/-what-does-the-layoutinflater-attachtoroot-parameter-meanBreena
Is this attachToRoot sort of the equivalent of addView? Like it just get's added to the view hierarchy of the parent which is like a container view or view group?Adigranth
S
6

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**

  1. When attachToRoot = false it means

Don't attach the child view to parent "Right Now", Add it later.

  1. 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).

Selffertilization answered 12/9, 2017 at 6:43 Comment(0)
T
3

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. 
Teriteria answered 9/9, 2019 at 8:33 Comment(0)
A
1

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.

Axle answered 11/3, 2014 at 14:1 Comment(2)
Consider a scenario where I have declared layouts in the xml and i am dynamically(progrmatically) genreating the textviews then what are all in my viewgroup and what is the parent in that(1st textview or the layout in the xml file)?Orotund
The ViewGroup that you want to contain the TextViews. If you have a LinearLayout in your xml, and you want the dynamically generated TextViews to be in that LinearLayout, pass that LinearLayout as the parent.Axle
N
0

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"

Needlepoint answered 16/5, 2022 at 3:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.