I don't know if this is possible, is it possible for me to inflate a listview using only its id? Thanks.
-
I think you should only inflate _layouts_ i.e. `ViewGroup`s. Maybe what you want is to set the visibility of a View from GONE to VISIBLE? – Laurent' Oct 06 '11 at 12:02
-
thanks laurent. yeah i tried inflating an xml file. that works. however, when i try expanding using the id of the root layout in this file, everything fails. – OckhamsRazor Oct 06 '11 at 12:13
-
Judging from the doc, it seems that the id has to be a xml file. Not a View. Could you please tell us why you need this to help find another solution? – Laurent' Oct 06 '11 at 12:18
-
i want to only inflate a specific portion of an xml file, and not the whole file. – OckhamsRazor Oct 06 '11 at 12:23
3 Answers
I think you may be confused about the difference between a component ID and a layout ID.
A layout ID refers to the name of a layout XML file. For example, if you have a layout named res/layout/home_activity.xml
, its ID would be stored as R.layout.home_activity
.
A component ID refers to the identity of a UI component inside an existing layout. So, inside your home_activity.xml
layout you might have <TextView android:id="@+id/my_textview" />
. The ID of that View is R.id.my_textview
.
You can only inflate layouts using a layout ID. 'Inflating' a component ID doesn't make any sense, unless you inflate a layout as a child of a View with a specific ID.

- 6,064
- 4
- 31
- 38
-
thanks dave. that really helped clear my confusion. but do you know how i can "inflate a layout as a child of a View with a specific ID"? an example would really help. – OckhamsRazor Oct 06 '11 at 12:25
-
Well, say you already have a reference to a (e.g.) `LinearLayout`, let's call it `root`. And say you've just inflated a layout, so you now have a `View` called `myInflatedLayout`. You could do `root.addView(myInflatedLayout)`. – Dave Oct 06 '11 at 12:41
Now that things are more clear, I suggest that you set your 'Inflatable ListView' as gone
in your Layout file using:
<ListView android:visibility="gone" ... />
Then just set it to visible using View.setVisibility()
whenever you want it to 'inflate'
mHiddenListView.setVisibility(View.VISIBLE);

- 2,611
- 1
- 14
- 22
Yes, the layout inflater inflates any View from the Resource id.
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.id.your_listview, parentView );
Edit: Well, it has to be a layout ID referring to some xml layout, not a component of it. Why don't you create a new xml containing that view component that you want to extract?

- 33,594
- 11
- 89
- 102
-
thanks reno. however, it says that the resource type is not valid. – OckhamsRazor Oct 06 '11 at 12:10
-
Sure? Quoting the doc: `resource: ID for an XML layout resource to load (e.g., R.layout.main_page)` – Laurent' Oct 06 '11 at 12:16
-
the resource id has to be a layout xml file. i however, want to expand only a specific layout within a larger xml file. – OckhamsRazor Oct 06 '11 at 12:31