0% found this document useful (0 votes)
160 views

Tablelayout - Android - Error in Adding Table Layout Dynamically - Stack Overflow

The document is a Stack Overflow post discussing errors encountered when dynamically adding a table layout to an Android XML file programmatically. The original poster receives errors when setting layout parameters and finds changing the parameter type from TableLayout.LayoutParams to FrameLayout.LayoutParams resolves the issue, but does not display anything. Another responder suggests also adding each row to the table after creation to properly display the content. When trying to add multiple rows in a loop, the poster then receives an error that the child view already has a parent, requiring removing it first before adding again.

Uploaded by

Sandeep Hari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

Tablelayout - Android - Error in Adding Table Layout Dynamically - Stack Overflow

The document is a Stack Overflow post discussing errors encountered when dynamically adding a table layout to an Android XML file programmatically. The original poster receives errors when setting layout parameters and finds changing the parameter type from TableLayout.LayoutParams to FrameLayout.LayoutParams resolves the issue, but does not display anything. Another responder suggests also adding each row to the table after creation to properly display the content. When trying to add multiple rows in a loop, the poster then receives an error that the child view already has a parent, requiring removing it first before adding again.

Uploaded by

Sandeep Hari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/26/2016

tablelayoutAndroid:ErrorinaddingtablelayoutdynamicallyStackOverflow

signup

login

tour

help

xDismiss

JointheStackOverflowCommunity
Stack Overflow is a community of 4.7 million
programmers, just like you, helping each other.
Join them it only takes a minute:
Signup

Android:Errorinaddingtablelayoutdynamically
IamtryingtodynamicallyaddtablelayouttoanxmlfileandtheLayoutParamsarecausingerrors.
Activitycode:
packagecom.pyrospiral.android.tabbedtimetable;
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.widget.TableLayout;
importandroid.widget.TableLayout;
importandroid.widget.TableLayout.LayoutParams;
importandroid.widget.TableRow;
importandroid.widget.TextView;

publicclassTimeTableWeekextendsActivity{
TextViewsubname;
TextViewtiming;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_table_week);
//Createtextview
TextViewtext=newTextView(this);
text.setText("aaaaa");
//Layoutparams
LayoutParamsparams=newTableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
android.widget.TableRow.LayoutParamstrparams=new
TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT);

http://stackoverflow.com/questions/28072215/androiderrorinaddingtablelayoutdynamically

1/3

9/26/2016

tablelayoutAndroid:ErrorinaddingtablelayoutdynamicallyStackOverflow

//Findtableinxml
TableLayouttable=(TableLayout)findViewById(R.id.gridtable);
table.setLayoutParams(params);
//Createtablerow
TableRowrow=newTableRow(this);
row.setLayoutParams(trparams);
row.setBackgroundColor(Color.GREEN);
row.addView(text);

}
}

XMLfile:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridtable">

</TableLayout>

</ScrollView>

Errors:
E/AndroidRuntimeFATALEXCEPTION:main
Process:com.pyrospiral.android.tabbedtimetable,PID:26691
java.lang.ClassCastException:android.widget.TableLayout$LayoutParamscannotbecastto
android.widget.FrameLayout$LayoutParams

ChangingTableLayout.LayoutParamstoFrameLayout.LayoutParamsgivesnoerrorsbutitalsodoesn't
showanything.
android tablelayout
editedJan21'15at16:45

askedJan21'15at16:32

Pyrospiral
13

2Answers

YouneedtochangeTableLayouttoFrameLayout,likethis:

http://stackoverflow.com/questions/28072215/androiderrorinaddingtablelayoutdynamically

2/3

9/26/2016

tablelayoutAndroid:ErrorinaddingtablelayoutdynamicallyStackOverflow

FrameLayout.LayoutParamsparams=newFrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);

toseeyourtextyouhavetoaddtherowtothetable
table.addView(row);
answeredJan21'15at16:47

kelvincer
1,180

18

Thanksthatworked,althoughifItryingdoingthatinaloopitsays"Thespecifiedchildalreadyhasaparent.

YoumustcallremoveView()onthechild'sparentfirst."Isthereaworkaroundtothis? Pyrospiral Jan21


'15at17:13
Read this answer.Theproblemisinyourcode.Maybeyouareaddingsamerowmultipletime,youneedto

createnewrow.kelvincerJan21'15at17:17

Table'slayoutparametersarealreadydefinedinyourXMLsoIthinkyourvariable"params"is
unnecessarythere.
answeredJan21'15at16:49

joan
65

http://stackoverflow.com/questions/28072215/androiderrorinaddingtablelayoutdynamically

3/3

You might also like