27

I only want to get an object from a xml layout file without having to implement it into the current layout.

I know the way with

LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true);

but after execution of the above the layout will be implemented and shown immediately inside my "myparent"-View, right? I only want to get the Object itself to get its attributes and such. And maybe (but only maybe) insert it later into the shown layout. Is that possible?

Regards

Matteo B.
  • 3,906
  • 2
  • 29
  • 43
  • What type of Object do you mean? If you just want a handle to an XML defined GUI object, why not just use findViewById(id)? – Andrew G Dec 12 '11 at 22:22

3 Answers3

50

You should change your line to:

LayoutInflater.from(context).inflate(R.layout.myfile, null);

You can find it in documentation here.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
6
LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true);

The end parameter determines whether or not to automatically add the new view to myparent. Turn it to false to still use the parent's layout attributes.

Or, if you don't care about the parent's layout params, follow @inazaruk's answer

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
-6

You could make this component invisible with:

android:visibility="gone"

Source: http://developer.android.com/reference/android/view/View.html#attr_android:visibility

Gorcyn
  • 2,807
  • 1
  • 20
  • 22