0

In the code below, correct me if I am wrong but we are calling the inflator,which I am assuming is a static var? That will inflate or convert the layout we pass in to a View type? But why and what is a container, and why do we pass in a true or false value?

View view=inflator.inflate(R.layout.layout_name,container, false); 
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
eli
  • 335
  • 1
  • 3
  • 18

2 Answers2

0

Container: the ViewGroup the view will be inserted into. For example, if this is an adapter for a ListView, it will the ListView. If this is a Fragment and your fragment container is FrameLayout it will be that FrameLayout. This is useful for using the LayoutParams of the container, because this might decide how the view you create will look like.

Boolean: This parameter is called attachToRoot. If true, it will do an addView() on the container after inflating, so your will be already in the container after calling this (and calling container.addView(view) will crash then). In adapters and fragments you should leave this on false, because the implementation of these will call addView() for you.

Also here's the original documentation if I was unclear somewhere.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
0

container is the parent. If you pass a parent, it will be inflated with the parent. The ViewGroup been asked for here as part of the inflate method parameters is used to inherit higher level styling. While passing null can seem harmless, it can actually cause serious problems for your app later on. Thus in that case view is inflated on its own and you might notice some design issues.

And boolean parameter decides, if the view is attached to to parent or not.

boolean: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Read more.

Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
  • ok, so what is parent, and what is a case in which we would want to attach a view to a parent? – eli Apr 03 '16 at 07:59
  • as mentioned by @DanielZolnai, "if this is an adapter for a `ListView`, it will the `ListView`. If this is a `Fragment` and your fragment container is `FrameLayout` it will be that `FrameLayout`." – Rohit Arya Apr 03 '16 at 08:03