Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 890
srujan maddula Avatar asked Mar 11 '14 12:03

srujan maddula


2 Answers

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

like image 82
Umer Farooq Avatar answered Oct 06 '22 00:10

Umer Farooq


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

like image 41
Rohit Singh Avatar answered Oct 06 '22 00:10

Rohit Singh