Reputation: 40721
I want to get a a childView
whose name is "name" inside a ViewGroup
. I knew that the nameView
is the second item inside the GroupView, so I used following code:
TextView nameView = (TextView)((ViewGroup)view).getChildAt(2); //2 is magic number
However, it is possible that ,in the future, I might add some other Views before the nameView
and above code will became invalid. So, I am wondering how could get the ChildView by name, something like:
TextView nameView = (TextView)((ViewGroup)view).getChildByID(R.id.name);
EDIT: thanks to kcoppock. I realize getChildByName("name")
does not make sense. So i will purse something like getChildByID
.
Upvotes: 0
Views: 437
Reputation: 12656
You method are looking for is View.findViewById(id). Per your example:
TextView nameView = (TextView) view.findViewByID(R.id.name);
(It is not necessary to cast to ViewGroup)
Barry
Upvotes: 5
Reputation: 134664
If you just need to find a particular child of a ViewGroup by its ID, just first get a reference to the ViewGroup:
ViewGroup vg = (ViewGroup)findViewById(R.id.myGroupLayout);
Then call findViewById()
on that ViewGroup:
View v = (TextView)vg.findViewById(R.id.name);
Upvotes: 1