bipeen bipeen -4 years ago 141
Java Question

How to dynamically add a TableRow in a Fragment?

I am new in Android programming, currently I am trying to insert a textview inside the table layout dynamically in fragment. It worked while extending the class to Activity. Below is my code snippet. Am I missing something?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_schedule, null);
TableLayout ll = (TableLayout) getActivity().findViewById(;
TableRow tr= new TableRow(getActivity());
TextView tv1 = new TextView(getActivity());
tv1.setText("TEST NUMBER");
tv1.setPadding(5, 5, 5, 5);
return view;

Thank you in advance.

Answer Source

you have to give view.findViewById, because you are adding that in the view.

TableLayout ll = (TableLayout) view.findViewById(;

Check this post. it will surely help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download