0

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 as well, and I feel as though I'm not doing it right.

The only time I say "inflate the xml" is when telling someone to write the code to use a widget from the xml in java code.

So if I see a button on someones layout, with the id of button01, and I tell them to "inflate the xml" I expect Button mybutton = (Button) findViewById(R.id.button01);

Is this wrong of me?

EDIT: It seems as though this is not the correct phrase. Does one exist?

EGHDK
  • 17,818
  • 45
  • 129
  • 204

3 Answers3

3

"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

   View view = inflator.inflate(R.layout.main)

After that you can access the objects using "findViewById"

    Button mybutton = (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"

AndroidGuy
  • 3,371
  • 1
  • 19
  • 21
1

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:

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

Another way to obtain the inflater would be:

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

You would then proceed with

Button mybutton = (Button) view.findViewById(R.id.button01);
cklab
  • 3,761
  • 20
  • 29
  • If my program consisted of 1 layout, 1 button, and 1 activity, what would be the point of `View view = getLayoutInflater().inflate(R.layout.mylayout);` That line is not needed to write `Button mybutton = (Button) view.findViewById(R.id.button01);` is it? Have I been doing something fundamentally incorrect this entire time? – EGHDK Jun 26 '12 at 23:44
  • No, you doing `setContentView(R.layout.mylayout)` has the same effect and inflates it for the `Activity` you will be using it for. The above code is for inflating other/additional layouts. – cklab Jun 26 '12 at 23:46
  • Forgive my stupidity... why would you want to have more than one layout? – EGHDK Jun 26 '12 at 23:48
  • 1
    @EGHDK In any case where you have more than one way of displaying data or more than one view etc. Think of a contacts application. There would be a contact list, and a contact details view at least. Each of these would need their own layouts – Chris Thompson Jun 26 '12 at 23:48
  • 1
    One place where I inflate a layout is in a custom Adapter for `ListView`s. The use case for me has been to inflate when I use a layout outside of an `Activity`. – cklab Jun 26 '12 at 23:49
0

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.

droider
  • 302
  • 1
  • 3
  • 11