-1

I've been struggling with finding an appropiate answer on how to add components to a layout in a fragment at runtime in android studio.

Specifically:

I have Class A which downloads and parses an XML file. Fragment B instantiates this Class A and should display those downloaded items. (just a textview for now)

This is the XML file in which the textView should be displayed. The items should be displayed in two columns. I know how to create the layout in the XML File but I have no idea on how to do it programatically. I've also read something about inflaters but I dont know if it fits this purpose.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:paddingTop="10dp">

            <TextView
                android:id="@+id/columnItem"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="5dp"
                android:layout_marginStart="5dp"
                android:layout_weight=".5"
                android:background="#c5c5c5"
                android:gravity="center"
                android:text="@string/CategoryLeft" />

        </TableRow>
    </ScrollView>
</TableLayout>

So here is the Code in Fragment B which currently just changes the text of the two existing textviews, that works just fine.

public void onStart() {
        super.onStart();
 
        ArrayList<String> categories = new ArrayList<>();
        XMLHandler getXML = new XMLHandler();
        getXML.execute();

        categories = getXML.getCategories();

        Iterator<String> it = categories.iterator();
        while (it.hasNext()) {
            System.out.println("Data is " + it.next());
            columnItem.setText(it.next());
        }
    }

The goal is to add a new TextView for each iteration through the while loop to the parent layout. This TextView should display the content of it.next().

Thanks in advance and let me know if u need any further information.

bautista
  • 765
  • 10
  • 26

1 Answers1

1

If you want to add a TextView to TableRow.

First, add an id to TableRow

    <TableRow
        android:id="@+id/table1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:paddingTop="10dp">

Then, in your onCreate

tableRow = findViewById(R.id.table1);  // tableRow is a global variable

add a void in your fragment

private void addTextView(String atext) {
    TextView txt = new TextView(getActivity());
    txt.setText(atext);
    tableRow.addView(txt);
    // here you can add other properties to the new TextView (txt)
}

and then

public void onStart() {
    super.onStart();

    ArrayList<String> categories = new ArrayList<>();
    XMLHandler getXML = new XMLHandler();
    getXML.execute();

    categories = getXML.getCategories();

    Iterator<String> it = categories.iterator();
    while (it.hasNext()) {
        String atxt = it.next();
        System.out.println("Data is " + atxt);
        addTextView(atxt);
    }
}
Ferran
  • 1,442
  • 1
  • 6
  • 10
  • Thanks this one worked for me. If somebody else comes across this problem: When accessing the tableRow from a Fragment, you have to call `View view = inflater.inflate(R.layout.fragment_layout, container, false);` and then `view.findViewById(R.id.table1);` in the onCreateView method of your Fragment, otherwise you'll get a nullpointer exception. – bautista Apr 15 '19 at 12:57