Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an inflated view to a linear layout

How do you inflate a view and add it to a the list of child views of a LinearLayout?

like image 658
mohan Avatar asked Sep 01 '11 12:09

mohan


1 Answers

When you call the inflater constructor, set attachToRoot to false; then manually add the view after you've initialized it. Otherwise, you'll lose your initialization for all but the first child added.

Example:

View view = inflater.inflate(R.layout.some_view, parent, false);
((TextView) view.findViewById(R.id.some_text)).setText(someString);
parent.addView(view);

An example of what not to do:

View view = inflater.inflate(R.layout.some_view, parent);
((TextView) view.findViewById(R.id.some_text)).setText(someString);
like image 123
Edward Brey Avatar answered Sep 19 '22 23:09

Edward Brey