Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get layout from view

Tags:

android

I am inflating interface from XML using

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

Now how can i access RelativeLayout from add_phone view? is there any methos like getChildCount() ?

like image 883
Lalit Chattar Avatar asked Oct 26 '11 07:10

Lalit Chattar


2 Answers

You can find child views of a view through

View.findViewById(int id)

In your case, that translates to

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

As long as you have unique id's for the child elements in add_phone, this should return the correct element.

like image 65
nanoquack Avatar answered Sep 22 '22 18:09

nanoquack


yes , getChildCount(), works on a ViewGroup like LinearLayout, RelativeLayout etc..

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

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

like image 26
Yashwanth Kumar Avatar answered Sep 22 '22 18:09

Yashwanth Kumar