tags:

views:

1070

answers:

1

This is a dumb question and I know the answer is sitting in front of me, I'm just having trouble searching for it in the right way.

I've got a custom view that has been set as the content view and inflated from xml. How can I access the instance to call methods on it from the activity class? I remember seeing something akin to getResourceById() a while back, but now I can't seem to find it and I'm not even sure if that's the best way to do it.

Sorry for the dumb question...

Thanks, Chris

+3  A: 

If you have used an inflater, you will be given an instance of a View class. You then use your instance like so

LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.small_listview_row, null);

TextView tvItemText = (TextView)row.findViewById(R.id.tvItemText);
MattC
Ah, that would make sense, so would manually inflating it and then setting the content view with that new instance be the best way to do things if I'm going to need access to that view?
Chris Thompson
Another alternative is just to put an android:id on the root element of the layout XML, then call findViewById() for it. Since the root View should be the first thing findViewById() checks, it will respond quickly.
CommonsWare
Whomever down-voted my answer should provide an explanation.
MattC