I've been developing in , for a little over a month Xamarin
, and despite having studied Android
, until now I haven't had the opportunity, or rather, the need to use LayoutInflater
.
I recently had a problem using, or abusing this feature. Solved in this question: Textview is not added to my layout , the problem came from using the LayoutInflater when it shouldn't.
I have read the documentation and it says that it instantiates an xml layout to its view
corresponding . Which leads me to think that when I "inflate" a layout I am creating an empty layout from that XML.
In this article they compare a Layoutinflater
with a beach inflator, I understand the metaphor that inside it goes XML
like gas and the beach ball would be the variable type vista
/ View
where that is loadedXML
This is so? I know it may seem simple, but there is something that squeaks me in all this.
What advantages can it have? When can it be more convenient for me to use Layoutinflater
a variable to assign a FindViewById()
and pass the layout directly?
You mean the LayoutInflater , actually inflater is the name of the variable that is generally used to inflate a layout in Android:
To establish a difference it is necessary to know the definitions:
Regarding your questions:
It's a good comparison, the "inflator" inflates the beach layout:
but inside this view (layout_playa) you can find elements like
pelota
,sombrilla
,lentes_sol
:To access these elements, you search in the view that you previously inflated by means of their id using FindViewById() , since these elements are inside the view:
It's important that the view references you get via FindViewById() must exist, otherwise you'll get null values.
Actually its use is different, LayoutInflater creates an instance of a layout XML file in its corresponding view objects and this view does not need to exist in the layout of your activity but in the resources while to use FindViewById() you use to search for views that are in a previously inflated layout:
Another example of LayoutInflater imagines that you have to "inflate" one type of cell when your list cell is even and another type when it is odd:
public override View GetView(int position, View convertView, ViewGroup parent) {
As an example of FindViewById() , you search inside a layout (
layout_app
) for the reference of a view (TextView
) by means of its id (Resource.Id.tv_titulo
) and modify some property, in this case you assign a text.The LayoutInflater , basically what it does is to have a reference/instantiate an axml layout to be able to use the views within it. When you go to load content on a screen, for example, a
Activity
, you doSetContentView
this to indicate that the content of that screen points to a specific layout. This means that the references that you are going to search forFindViewById
in that Activity should only be attached to that layout that you have loaded.Normally, the main view of a Layout is a ViewGroup (for example,
LinearLayout
,FrameLayout
, etc...), a layout type parent that contains children, so if you try to makeFindViewById
a view or child that does not belong to that ViewGroup , for example, that it is in another axml layout than the one loaded for a specific screenActivity
, then it will return a reference to younull
.Let's take the following example: Let's say you want to create different screens. Using the same layout that contains a
Button
with the idbutton
and aTextView
with the idtextView
. Doing this via axml has many alternatives, such as using the include tag . However, if you want to do it programmatically, you should use LayoutInflater here.For example:
Xamarin.Android (C#)
, using LayoutInflater in aActivity
:This way you already have that layout loaded inside your
LinearLayout
. If you want to access thatButton
and theTextView
to modify them, you can simply do:and you can now customize it and assign the properties you want.