Problem to add View dynamically in Linearlayout
15,421
Solution 1
I have add the orientation of LinearLayout as Vertical. And it become perfect. Thank you Sujit for your hints.
Solution 2
Use the version of addView()
that takes a LayoutParams
object, supply an appropriate LinearLayout.LayoutParams
, and see if that helps.
Related videos on Youtube
Author by
dev_android
Updated on August 06, 2022Comments
-
dev_android almost 2 years
I want to add multiple View in a
LinearLayout
. Here is the code I am using for add adding multiple view to theLinearLayout
.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?
-
Sujit almost 13 yearshave you set the orientation of the the LinearLayout ?
-
-
dev_android almost 13 yearsI tried with " seriesMainListItemView.addView(inflatedView,i,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));" also, no change in result
-
CommonsWare almost 13 years@dalandroid: Use Hierarchy View to see if the widgets are being added. If they are not, determine why your code is not being called or something. If they are, Hierarchy View should indicate why they are not visible.
-
Rock Lee over 8 yearsThis solved my problem! I was trying to make a custom android component and forgot to set the orientation on my new class that extends LinearLayout.