How to add TableRow from xml to TableLayout programmatically?
Asked Answered
P

1

8

Im new to Android and I'm trying to add a TableRow, which I already made with xml, to a TableLayout programmatically. I'm getting Force Closes, most are NullPointerExcpetions.

Here's my java class

public class DayFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.subjects_list, container, false);


    TableLayout tl = (TableLayout) view.findViewById(R.id.tl);
    TableRow tr = (TableRow) view.findViewById(R.id.tr_row);

            tl.addView(tr); //not working, obviously im missing something

    //xml parsing stuff

            return view;
 }
}

This is my layout with the TableLayout:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dp" >

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/header"
            layout="@layout/table_header" />

        <include android:id="@+id/h_divider"
            layout="@layout/horizontal_divider"/>




    </TableLayout>
</ScrollView>

</HorizontalScrollView>

And the TableRow:

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

<TextView
    android:id="@+id/tv_hour"
    android:layout_weight="1"
    android:gravity="center"
    android:padding="8dp"
    android:textSize="15sp"
    android:textStyle="bold" />

<View
    android:layout_weight="1"
    android:background="@drawable/vertical_cell_divider" />

<TextView
    android:id="@+id/tv_subject"
    android:layout_weight="1"
    android:gravity="left"
    android:padding="8dp"
    android:textSize="18sp" />

<View
    android:layout_weight="1"
    android:background="@drawable/vertical_cell_divider" />

<TextView
    android:id="@+id/tv_start"
    android:layout_weight="1"
    android:gravity="left"
    android:padding="8dp"
    android:textSize="18sp" />

<TextView
    android:id="@+id/tv_bar"
    android:layout_weight="1"
    android:gravity="left"
    android:textSize="18sp" />

<TextView
    android:id="@+id/tv_end"
    android:layout_weight="1"
    android:gravity="left"
    android:padding="8dp"
    android:textSize="18sp" />

</TableRow>

I tried lots of different ways to achieve it but still nothing.

Presbyterate answered 3/1, 2013 at 12:17 Comment(0)
B
10

Replace code:

TableRow tr = (TableRow) view.findViewById(R.id.tr_row);
tl.addView(tr); //not working, obviously im missing something

with

View tr = inflater.inflate(R.layout.table_row_layout, null,false);
tl.addView(tr); //not working, obviously im missing something
Bloodsucker answered 3/1, 2013 at 12:20 Comment(4)
YES! Thank you very much for solving my problem. Can you also tell me how to add the same view more than once please? The child already has a parent so I can't add it more by just putting tl.addView(tr);Presbyterate
repeat the code inside for loop. but keep in mind that you have create view array before repeating in for loop. then for every view in array inflate the table_row_layout and add it to Table Layout.Bloodsucker
I just did it like this and it worked for (int i=0;i<7;i++){ View tr = inflater.inflate(R.layout.subject_row, null, false); tl.addView(tr); }Presbyterate
@Bloodsucker can u please help me i have tablelayout and then tablerow which i added using xml and its working but inside tablerow i have another tablelayout and then tablerow of xml i am facing difficulty to add second tablerow of xml can u please help meVocative

© 2022 - 2024 — McMap. All rights reserved.