Skip to content Skip to sidebar Skip to footer

What Is The Correct Term When Calling A Widget From Xml In Android?

I'm still very new to Android, but I am trying to keep up by using tutorials on YouTube. A bunch of people throw around the phrase 'inflate the xml'. I started to use this phrase a

Solution 1:

"Inflating" a layout refers to the process of having the Android framework convert a layout in XML format into objects corresponding to the different views in the layout.

To "inflate" a layout you need:

  • a layout in XML format

    res/layout/main.xml

  • access to an inflator object

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

You then need to run the inflation on the layout

Viewview= inflator.inflate(R.layout.main)

After that you can access the objects using "findViewById"

Buttonmybutton= (Button) view.findViewById(R.id.button01);

The Activity class provides a helper method which both gets the inflator and inflates the layout

setContentView(R.layout.main)

When using the "setContentView" method, the activity sets a default view which is used when calling "findViewById"

Solution 2:

Sort of. When someone says to inflate a layout (or as you say, inflate the xml), the piece of code that generally comes to mind is something like:

Viewview= getLayoutInflater().inflate(R.layout.mylayout, null);

Another way to obtain the inflater would be:

LayoutInflaterinflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

You would then proceed with

Buttonmybutton= (Button) view.findViewById(R.id.button01);

Solution 3:

When you are using Button mybutton = (Button) findViewById(R.id.button01);, you are basically casting the button you created in your code to the button you defined in the xml.

I don't really know if "inflating" is the correct term. I think of it as a way to link what you have placed in your xml layout with your functionality you have defined in your java code.

Post a Comment for "What Is The Correct Term When Calling A Widget From Xml In Android?"