0

I've a simple task - add child views to a LinearLayout dynamically, but I don't know, how to do it in a proper way.

There are two ways:

1. Inflate view and passed parent container to the inflate method.

View view = LayoutInflater.from(getActivity()).inflate(R.layout.item, container, true);

At this case, when I've more than one child, LayoutInfater returns the same child view object every time, when inflate it. So I can proper initialized other child views.

2. The next way is to use LinearLayout method addView(View view).

The problem is, that the child view lose it's LayoutParams state. And I must set new LayoutParams for child programmatically. It's a not good practice.

Also we can put child in the complimentary wrapper layout in his own layout resource file. But it's also a not good practice put layout to the wrapper.

My child resource:

<RelativeLayout android:gravity="center_vertical"
            android:paddingLeft="25dp"
            android:layout_width="match_parent"
            android:layout_height="@dimen/list_item_height">

<TextView android:id="@+id/tv_name"
          android:text="Name"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>

So what is the proper way to add child to LinearLayout, without adding LayoutParams programmatically and without wrapper layout?

Thank's a lot for help!

harmashalex
  • 240
  • 2
  • 12
  • You can check the question. Question has linear layout as a parent and adding tableLayout as a child views. http://stackoverflow.com/questions/31847066/top-view-disappeared-in-scrollview – Saritha G Aug 11 '15 at 09:38
  • At this sample, root child layout hasn't custom layout params. (such as fixed layout_width or layout_height) – harmashalex Aug 11 '15 at 09:45
  • Do you want to fix the child view width and height?? or you don't want to fix it? Linear layout is root layout and thatz not custom. – Saritha G Aug 11 '15 at 09:49
  • http://stackoverflow.com/questions/2335813/how-to-inflate-one-view-with-a-layout – IntelliJ Amiya Aug 11 '15 at 09:54
  • When your child's root layout has, for example, layout_height="50dp", so when yout add this child to container, such as container.addView(child), child's layout_height set as wrap_content, because it is LayoutParams parameter. – harmashalex Aug 11 '15 at 09:56

3 Answers3

0

Did you try this way?

View view= LayoutInflater.from(getActivity()).inflate(R.layout.item, null, false);

parentView.addView(view);
Adam Fręśko
  • 1,064
  • 9
  • 14
0

if you want to add child to a RelativeLayout,you need to set LayoutParams for child view ,and if you want to add child to a LinerLayout,you don't need to set LayoutParams,so if you want to custom layoutParams,i suggest you to use RelativeLayout,or you can make a wrapper view for child View ,just like:

//inflate a wrapper view
View child = LayoutInflater.from(getActivity()).inflate(R.layout.item, container, false);
//add to a linerLayout
LinerLayout.addChild(child)
0

Try this way,

View view = LayoutInflater.from(getActivity()).inflate(R.layout.item, **container**, false);
layout.addView(view);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Alex.Li
  • 540
  • 1
  • 4
  • 12