android - Setting table layout through Java dynamically -
i need screen have couple of text views defined in activity layout. below views want set table number of rows , columns depend upon user's input (dynamic).
this code in oncreate of activity
super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home_page); tablelayout tablelayout = new tablelayout(getapplicationcontext()); tablerow tablerow; textview textview; (int = 0; < no_days; i++) { tablerow = new tablerow(getapplicationcontext()); (int j = 0; j < no_subjects; j++) { textview = new textview(getapplicationcontext()); textview.settext("hello"); textview.setpadding(20, 20, 20, 20); tablerow.addview(textview); } tablelayout.addview(tablerow); } setcontentview(tablelayout);
but code taking whole screen , unable have text views have defined in activity layout.
here's activity_home_page.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.attendancerecord.homepage" tools:ignore="mergerootframe" > <textview android:id="@+id/textview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="10dp" android:text="@string/welcome" android:textappearance="?android:attr/textappearancelarge" /> <textview android:id="@+id/display_user_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview5" android:layout_centerhorizontal="true" android:layout_margintop="33dp" android:textappearance="?android:attr/textappearancemedium" /> </relativelayout>
if want tablelayout
below textviews
use linearlayout
orientation "vertical
" in activity_home_page
instead of relativelayout
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.attendancerecord.homepage" tools:ignore="mergerootframe" android:orientation="vertical" > <textview android:id="@+id/textview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp" android:text="welcome" android:textappearance="?android:attr/textappearancelarge" /> <textview android:id="@+id/display_user_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="33dp" android:textappearance="?android:attr/textappearancemedium" /> </linearlayout>
and add mainlinear.addview(tablelayout);
@ end of loop
linearlayout mainlinear = (linearlayout) findviewbyid(r.id.container); (int = 0; < 5; i++) { tablerow = new tablerow(getapplicationcontext()); (int j = 0; j < 4; j++) { textview = new textview(getapplicationcontext()); textview.settext("hello"); textview.setpadding(20, 20, 20, 20); tablerow.addview(textview); } tablelayout.addview(tablerow); } mainlinear.addview(tablelayout);
and remove setcontentview(tablelayout);
hope works
Comments
Post a Comment