Prateek Khandelwal Prateek Khandelwal - 3 months ago 20
Android Question

Adding Views dynamically in Linear Layout in Fragment

I am trying to add TextView to the nested linearLayout in fragmentin onCreateView, I don't know if this is the correct approach or not, new to android!
Thanks in advance

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
List<LifeBerrysXmlParser.Item> items = (ArrayList<LifeBerrysXmlParser.Item>) getArguments().getSerializable("List");
TextView mainHeading = (TextView) rootView.findViewById(R.id.mainHeading);
ImageView mainImage = (ImageView) rootView.findViewById(R.id.articleMainImage);
LinearLayout articleLayout = (LinearLayout) rootView.findViewById(R.id.articleLayout);
TextView tev = new TextView(getActivity());
tev.setText("Hello..............");
articleLayout.addView(tev);
LifeBerrysXmlParser.Item item = items.get(getArguments().getInt("position"));
String articleMainHeading = item.mainHeading;
String articlemainImage = item.mainImage;
mainHeading.setText(articleMainHeading);
if (!articlemainImage.isEmpty()) {
Picasso.with(getContext()).load(articlemainImage).into(mainImage);
}
return rootView;
}


can anyone please help me what I am doing wrong?

here is my xml for the fragment

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:id="@+id/content"
android:padding="1dp"
android:layout_weight="1"
android:background="#000000"
android:layout_gravity="center">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:scrollIndicators="right">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/articleMainImage"
android:maxHeight="300dp"/>
<TextView
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightLarge"
android:layout_weight="1"
android:lineSpacingMultiplier="1.2"
android:id="@+id/mainHeading"
android:textColor="#ff6435"
android:clickable="false"
android:layout_gravity="center"
android:layout_centerInParent="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/articleLayout"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read More"
android:textSize="15dp"
android:textStyle="bold"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:gravity="center"
android:onClick="readMore"
android:padding="1dp"
android:textColor="@android:color/holo_green_light" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Answer

You have mistake in your article layout. Your article layout has horizontal orientation, and it's child TextView has match_parent width, so when you add new child, it'll be outside the screen. Change orientation to vertical:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/articleLayout"
            android:orientation="vertical">      <---- Change this line
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Read More"
                android:textSize="15dp"
                android:textStyle="bold"
                android:layout_gravity="center"
                android:clickable="true"
                android:focusable="false"
                android:gravity="center"
                android:onClick="readMore"
                android:padding="1dp"
                android:textColor="@android:color/holo_green_light" />
        </LinearLayout>