Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem to add View dynamically in Linearlayout

I want to add multiple View in a LinearLayout. Here is the code I am using for add adding multiple view to the LinearLayout.

Java code:

LinearLayout seriesMainListItemView = (LinearLayout) findViewById(R.id.SeriesMainListItemView);
                LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                for (int i=0; i<scheduleArr.length; i++) {

                      View inflatedView = mInflater.inflate(R.layout.scheduleitem, null);
                      TextView inflatertext1 = (TextView) inflatedView.findViewById(R.id.text1);
                      TextView inflatertext2 = (TextView) inflatedView.findViewById(R.id.text2);
                      inflatertext1.setText(scheduleArr[i][0]);
                      inflatertext2.setText(scheduleArr[i][1]);
                      Log.i("data",i + " " + scheduleArr[i][0] + "/" + scheduleArr[i][1]);
                      seriesMainListItemView.addView(inflatedView);
                }

Here is the View xml I want to add multiple times.


Here is the LinearLayout where I want to add it.

<TableLayout
                android:layout_gravity="center_vertical" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:paddingLeft="1dip"
                android:paddingRight="1dip" >
                 <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
                    <ImageView
                       android:id="@+id/imgSeriesMainScheduleImg"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_gravity="center"
                       android:background="@drawable/scheduleheader"/> 
                    <LinearLayout  
                        android:id="@+id/SeriesMainListItemView"                                    
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">
                    </LinearLayout>                                                                                              
                </TableRow>
..............
</TableLayout>

But only single view is adding in the LinearLayout, although length of the array is 3. What is the problem in my code?

like image 256
dev_android Avatar asked Jul 20 '11 11:07

dev_android


People also ask

Is this possible to add views in a layout dynamically in android?

Yes, because you can not add/delete any View Type in XML on runtime. Dynamic layouts are developed using Java and can be used to create layouts that you would normally create using an XML file.

What is the difference between RelativeLayout and LinearLayout?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

What is the correct syntax of LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

Can we use linear layout in RelativeLayout inside?

We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.


2 Answers

I have add the orientation of LinearLayout as Vertical. And it become perfect. Thank you Sujit for your hints.

like image 139
dev_android Avatar answered Sep 18 '22 17:09

dev_android


Use the version of addView() that takes a LayoutParams object, supply an appropriate LinearLayout.LayoutParams, and see if that helps.

like image 32
CommonsWare Avatar answered Sep 16 '22 17:09

CommonsWare