Get layout from view

I inflate an interface from XML using

View add_phone = getLayoutInflater().inflate(R.layout.phone_info, null); 

Now, how can I access the RelativeLayout from the add_phone view? are there any methods like getChildCount() ?

+7
source share
2 answers

yes, getChildCount (), works in a ViewGroup, e.g. LinearLayout, RelativeLayout, etc.

 ViewGroup add_phone = (ViewGroup) getLayoutInflater().inflate(R.layout.phone_info, null); int childCount = add_phone.getChildCount(); 

you must make sure that the bloated layout has viewGroup as the parent view, otherwise you will get a class exception. viewGroup can be like LinearLayout, RelativeLayout, etc.

+6
source

You can find child views of the view through

 View.findViewById(int id) 

In your case, this means

 RelativeLayout child = (RelativeLayout)add_phone.findViewById(R.layout.phone_info) 

As long as you have a unique identifier for the children in add_phone, it should return the correct element.

+4
source

All Articles