0

I need my screen to have a couple of text views defined in the activity layout. Below those views I want to set a table whose number of rows and columns will depend upon the user's input (dynamic).

This code is in the oncreate of the activity

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);

    TableLayout tableLayout = new TableLayout(getApplicationContext());
    TableRow tableRow;
    TextView textView;

    for (int i = 0; i < no_days; i++) {
        tableRow = new TableRow(getApplicationContext());
        for (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 this code is taking the whole screen and I am unable to have those text views which I have defined in the activity layout.

Here's my 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>
2
  • Post your activity_home_page Commented Jun 8, 2014 at 20:02
  • Yes activity_home_page.xml Commented Jun 8, 2014 at 20:08

2 Answers 2

1

If you only want your TableLayout to below your TextViews then use LinearLayout with 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); at the end of your loop

LinearLayout  mainLinear = (LinearLayout) findViewById(R.id.container);

        for (int i = 0; i < 5; i++) {
            tableRow = new TableRow(getApplicationContext());
            for (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 this works

Sign up to request clarification or add additional context in comments.

Comments

0

Add this to your activity file:

<TableLayout
     android:id="@+id/table1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/display_user_name"
    android:layout_marginTop="15dp" >

</TableLayout>

And in your class :

TableLayout tableLayout = (TableLayout)findViewById(R.id.table1);
         TableRow tableRow;
         TextView textView;
            for (int i = 0; i < no_days; i++) {
                tableRow = new TableRow(getApplicationContext());
                for (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);
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.