Add view to another programmatically inside a fragment

Something is wrong with my code. I have an XML representing the layout of my fragment (fragment_layout.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical" >

</LinearLayout>

      

and another xml file representing one view which is inside the fragment layout (element.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" />

<EditText
    android:id="@+id/et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:inputType="number"
    android:text="0" />

</RelativeLayout>   

      

and this is how I populate the fragment layout inside the onCreateView method:

View view = inflater.inflate(R.layout.fragment_layout, container,
            false);

    some_elements = getActivity().getResources().getStringArray(
            R.array.some_array);

    LinearLayout root = (LinearLayout) view.findViewById(R.id.root_layout);

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

        View element = inflater.inflate(
                R.layout.element, container, false);

        TextView tv= (TextView) element.findViewById(R.id.tv);
        prodotto.setText(prodotti[i]);

        EditText et= (EditText) element.findViewById(R.id.et);

        root.addView(element);

    }

    return view; 

      

Now it will happen that only the first element of the "some_elements" array is shown inside the fragment (the element contains 10 elements). why are other items not showing? what's wrong?

+3


source to share


1 answer


The problem is android:layout_height="match_parent"

, in your RelativeLayout. It should bewrap_content



+3


source







All Articles