Static buttons under a ScrollView with a dynamic tablelayout within it





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a dynamic table that can have as few as 2 rows but can also have upto 50 rows. I am trying to wrap that table within a scrollview. I also need to have 2 buttons that should show up at the bottom of the page.



Here is what I am trying to achieve but am getting stuck (attached the images to show what I am trying):




  1. If there are few rows, there shouldnt be a scroll view and the table should appear with the buttons just below the table.

  2. If there are more than x number of rows, wrap that table in a scrollview with a max height and then show the buttons at the bottom at a static place. The table scrolls but the buttons disappear below the scrollview.


Can someone suggest what I am doing wrong ? I scanned through a lot of questions on StackOverflow but couldnt find something that addresses this particular situation.



Below is my layout file and I populate the table in the activity :



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/mainTable">
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="425dp"
android:layout_gravity="center_horizontal"
android:text="Submit" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>


Table and buttons appear fine when there are few rows



Max Height for the table with buttons



Buttons Disappear



RelativeLayout



enter image description here










share|improve this question































    1















    I have a dynamic table that can have as few as 2 rows but can also have upto 50 rows. I am trying to wrap that table within a scrollview. I also need to have 2 buttons that should show up at the bottom of the page.



    Here is what I am trying to achieve but am getting stuck (attached the images to show what I am trying):




    1. If there are few rows, there shouldnt be a scroll view and the table should appear with the buttons just below the table.

    2. If there are more than x number of rows, wrap that table in a scrollview with a max height and then show the buttons at the bottom at a static place. The table scrolls but the buttons disappear below the scrollview.


    Can someone suggest what I am doing wrong ? I scanned through a lot of questions on StackOverflow but couldnt find something that addresses this particular situation.



    Below is my layout file and I populate the table in the activity :



    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
    android:id="@+id/sv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TableLayout android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/mainTable">
    </TableLayout>
    </ScrollView>
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="425dp"
    android:layout_gravity="center_horizontal"
    android:text="Submit" />

    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_gravity="center_horizontal"
    android:text="Cancel" />
    </LinearLayout>
    </LinearLayout>


    Table and buttons appear fine when there are few rows



    Max Height for the table with buttons



    Buttons Disappear



    RelativeLayout



    enter image description here










    share|improve this question



























      1












      1








      1








      I have a dynamic table that can have as few as 2 rows but can also have upto 50 rows. I am trying to wrap that table within a scrollview. I also need to have 2 buttons that should show up at the bottom of the page.



      Here is what I am trying to achieve but am getting stuck (attached the images to show what I am trying):




      1. If there are few rows, there shouldnt be a scroll view and the table should appear with the buttons just below the table.

      2. If there are more than x number of rows, wrap that table in a scrollview with a max height and then show the buttons at the bottom at a static place. The table scrolls but the buttons disappear below the scrollview.


      Can someone suggest what I am doing wrong ? I scanned through a lot of questions on StackOverflow but couldnt find something that addresses this particular situation.



      Below is my layout file and I populate the table in the activity :



      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

      <ScrollView
      android:id="@+id/sv"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <TableLayout android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:id="@+id/mainTable">
      </TableLayout>
      </ScrollView>
      <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
      <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="425dp"
      android:layout_gravity="center_horizontal"
      android:text="Submit" />

      <Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:layout_gravity="center_horizontal"
      android:text="Cancel" />
      </LinearLayout>
      </LinearLayout>


      Table and buttons appear fine when there are few rows



      Max Height for the table with buttons



      Buttons Disappear



      RelativeLayout



      enter image description here










      share|improve this question
















      I have a dynamic table that can have as few as 2 rows but can also have upto 50 rows. I am trying to wrap that table within a scrollview. I also need to have 2 buttons that should show up at the bottom of the page.



      Here is what I am trying to achieve but am getting stuck (attached the images to show what I am trying):




      1. If there are few rows, there shouldnt be a scroll view and the table should appear with the buttons just below the table.

      2. If there are more than x number of rows, wrap that table in a scrollview with a max height and then show the buttons at the bottom at a static place. The table scrolls but the buttons disappear below the scrollview.


      Can someone suggest what I am doing wrong ? I scanned through a lot of questions on StackOverflow but couldnt find something that addresses this particular situation.



      Below is my layout file and I populate the table in the activity :



      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

      <ScrollView
      android:id="@+id/sv"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <TableLayout android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:id="@+id/mainTable">
      </TableLayout>
      </ScrollView>
      <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
      <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="425dp"
      android:layout_gravity="center_horizontal"
      android:text="Submit" />

      <Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:layout_gravity="center_horizontal"
      android:text="Cancel" />
      </LinearLayout>
      </LinearLayout>


      Table and buttons appear fine when there are few rows



      Max Height for the table with buttons



      Buttons Disappear



      RelativeLayout



      enter image description here







      android android-layout scrollview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 22:17







      Prashanth

















      asked Nov 16 '18 at 21:43









      PrashanthPrashanth

      1,08911018




      1,08911018
























          2 Answers
          2






          active

          oldest

          votes


















          2














          I would consider using a RecyclerView instead of a TableLayout within a ScrollView. If you can set a static height for your button view layout you can manipulate the padding and margins to get the desired effect. See below modified from this answer: https://stackoverflow.com/a/45161728/6723646



          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

          <ScrollView
          android:id="@+id/sv"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:paddingBottom="45dp">
          <TableLayout android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:id="@+id/mainTable">
          </TableLayout>
          </ScrollView>
          <LinearLayout
          android:layout_marginTop="-45dp"
          android:layout_width="wrap_content"
          android:layout_height="45dp"
          android:orientation="horizontal"
          android:layout_gravity="center">
          <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:text="Submit" />

          <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="10dp"
          android:layout_gravity="center_horizontal"
          android:text="Cancel" />
          </LinearLayout>
          </LinearLayout>





          share|improve this answer


























          • Thanks @airfish. I am not sure how to align a table layout with a recyclerview and attach it dynamically from an activity.

            – Prashanth
            Nov 16 '18 at 22:23











          • I suppose you could use the same concept with your TableLayout embedded in the ScrollView instead of the RecyclerView above. The ScrollView would have the same properties as the RecyclerView and the TableLayout would stay the same.

            – rhpekarek
            Nov 16 '18 at 22:33











          • Please see edited xml

            – rhpekarek
            Nov 16 '18 at 22:39











          • Tried this - one problem - the scroll bar appears right next to the second column instead of being to the extreme right.

            – Prashanth
            Nov 16 '18 at 22:44











          • Try making the scrollview width match parent and table layout with layout gravity center in parent

            – rhpekarek
            Nov 16 '18 at 22:58





















          0














          Thanks @airfish and @Onik for your solutions. The solution from @airfish helped resolve my issue finally, after struggling with it for the past 2 days.



          Here is the final solution, for anyone interested in getting this to work.



          Final Layout File.



          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

          <ScrollView
          android:id="@+id/sv"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:paddingBottom="45dp">
          <TableLayout android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:gravity="center_horizontal"
          android:id="@+id/mainTable">
          </TableLayout>
          </ScrollView>
          <LinearLayout
          android:layout_marginTop="-45dp"
          android:layout_width="wrap_content"
          android:layout_height="45dp"
          android:orientation="horizontal"
          android:layout_gravity="center">
          <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:text="Submit" />

          <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="10dp"
          android:layout_gravity="center_horizontal"
          android:text="Cancel" />
          </LinearLayout>
          </LinearLayout>


          MainActivity.java (Edit Line # 30 to increase/decrease the number of rows for testing)



          package com.example.scroll_ex;

          import android.app.Activity;
          import android.os.Bundle;
          import android.view.Gravity;
          import android.widget.TableLayout;
          import android.widget.TableRow;
          import android.widget.TextView;

          public class MainActivity extends Activity {
          @Override
          public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          TableLayout mainTable = findViewById(R.id.mainTable);

          mainTable.removeAllViews();

          int leftRowMargin = 0;
          int topRowMargin = 0;
          int rightRowMargin = 0;
          int bottomRowMargin = 0;
          int textSize = 0, smallTextSize = 0, mediumTextSize = 0;

          TableRow tr = new TableRow(this);
          TextView column1 = new TextView(this);
          TextView column2 = new TextView(this);

          for(int i=-1; i < 25; i++){

          tr = new TableRow(this);
          column1 = new TextView(this);
          column2 = new TextView(this);

          tr.setGravity(Gravity.CENTER);
          tr.setId(i+ 1);
          TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
          TableLayout.LayoutParams.WRAP_CONTENT);
          trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
          tr.setPadding(0,0,0,0);
          tr.setLayoutParams(trParams);

          if (i == -1) {
          column1.setText("Name");
          column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
          TableRow.LayoutParams.MATCH_PARENT));
          column1.setPadding(5, 5, 1, 5);
          } else {
          column1.setText("Name #" + i);
          column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
          TableRow.LayoutParams.WRAP_CONTENT));
          column1.setPadding(5, 0, 1, 5);
          }

          tr.addView(column1);


          if (i == -1) {
          column2.setText("Address");
          column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
          TableRow.LayoutParams.MATCH_PARENT));
          column2.setPadding(5, 5, 1, 5);
          } else {
          column2.setText("Address " + i);
          column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
          TableRow.LayoutParams.WRAP_CONTENT));
          column2.setPadding(5, 0, 1, 5);
          }

          tr.addView(column2);
          mainTable.addView(tr, trParams);
          }

          }
          }


          Scroll with buttons



          No Scroll






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345826%2fstatic-buttons-under-a-scrollview-with-a-dynamic-tablelayout-within-it%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            I would consider using a RecyclerView instead of a TableLayout within a ScrollView. If you can set a static height for your button view layout you can manipulate the padding and margins to get the desired effect. See below modified from this answer: https://stackoverflow.com/a/45161728/6723646



            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ScrollView
            android:id="@+id/sv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="45dp">
            <TableLayout android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/mainTable">
            </TableLayout>
            </ScrollView>
            <LinearLayout
            android:layout_marginTop="-45dp"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:layout_gravity="center">
            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Submit" />

            <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center_horizontal"
            android:text="Cancel" />
            </LinearLayout>
            </LinearLayout>





            share|improve this answer


























            • Thanks @airfish. I am not sure how to align a table layout with a recyclerview and attach it dynamically from an activity.

              – Prashanth
              Nov 16 '18 at 22:23











            • I suppose you could use the same concept with your TableLayout embedded in the ScrollView instead of the RecyclerView above. The ScrollView would have the same properties as the RecyclerView and the TableLayout would stay the same.

              – rhpekarek
              Nov 16 '18 at 22:33











            • Please see edited xml

              – rhpekarek
              Nov 16 '18 at 22:39











            • Tried this - one problem - the scroll bar appears right next to the second column instead of being to the extreme right.

              – Prashanth
              Nov 16 '18 at 22:44











            • Try making the scrollview width match parent and table layout with layout gravity center in parent

              – rhpekarek
              Nov 16 '18 at 22:58


















            2














            I would consider using a RecyclerView instead of a TableLayout within a ScrollView. If you can set a static height for your button view layout you can manipulate the padding and margins to get the desired effect. See below modified from this answer: https://stackoverflow.com/a/45161728/6723646



            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ScrollView
            android:id="@+id/sv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="45dp">
            <TableLayout android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/mainTable">
            </TableLayout>
            </ScrollView>
            <LinearLayout
            android:layout_marginTop="-45dp"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:layout_gravity="center">
            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Submit" />

            <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center_horizontal"
            android:text="Cancel" />
            </LinearLayout>
            </LinearLayout>





            share|improve this answer


























            • Thanks @airfish. I am not sure how to align a table layout with a recyclerview and attach it dynamically from an activity.

              – Prashanth
              Nov 16 '18 at 22:23











            • I suppose you could use the same concept with your TableLayout embedded in the ScrollView instead of the RecyclerView above. The ScrollView would have the same properties as the RecyclerView and the TableLayout would stay the same.

              – rhpekarek
              Nov 16 '18 at 22:33











            • Please see edited xml

              – rhpekarek
              Nov 16 '18 at 22:39











            • Tried this - one problem - the scroll bar appears right next to the second column instead of being to the extreme right.

              – Prashanth
              Nov 16 '18 at 22:44











            • Try making the scrollview width match parent and table layout with layout gravity center in parent

              – rhpekarek
              Nov 16 '18 at 22:58
















            2












            2








            2







            I would consider using a RecyclerView instead of a TableLayout within a ScrollView. If you can set a static height for your button view layout you can manipulate the padding and margins to get the desired effect. See below modified from this answer: https://stackoverflow.com/a/45161728/6723646



            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ScrollView
            android:id="@+id/sv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="45dp">
            <TableLayout android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/mainTable">
            </TableLayout>
            </ScrollView>
            <LinearLayout
            android:layout_marginTop="-45dp"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:layout_gravity="center">
            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Submit" />

            <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center_horizontal"
            android:text="Cancel" />
            </LinearLayout>
            </LinearLayout>





            share|improve this answer















            I would consider using a RecyclerView instead of a TableLayout within a ScrollView. If you can set a static height for your button view layout you can manipulate the padding and margins to get the desired effect. See below modified from this answer: https://stackoverflow.com/a/45161728/6723646



            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ScrollView
            android:id="@+id/sv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="45dp">
            <TableLayout android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/mainTable">
            </TableLayout>
            </ScrollView>
            <LinearLayout
            android:layout_marginTop="-45dp"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:layout_gravity="center">
            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Submit" />

            <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center_horizontal"
            android:text="Cancel" />
            </LinearLayout>
            </LinearLayout>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 22:38

























            answered Nov 16 '18 at 22:04









            rhpekarekrhpekarek

            364




            364













            • Thanks @airfish. I am not sure how to align a table layout with a recyclerview and attach it dynamically from an activity.

              – Prashanth
              Nov 16 '18 at 22:23











            • I suppose you could use the same concept with your TableLayout embedded in the ScrollView instead of the RecyclerView above. The ScrollView would have the same properties as the RecyclerView and the TableLayout would stay the same.

              – rhpekarek
              Nov 16 '18 at 22:33











            • Please see edited xml

              – rhpekarek
              Nov 16 '18 at 22:39











            • Tried this - one problem - the scroll bar appears right next to the second column instead of being to the extreme right.

              – Prashanth
              Nov 16 '18 at 22:44











            • Try making the scrollview width match parent and table layout with layout gravity center in parent

              – rhpekarek
              Nov 16 '18 at 22:58





















            • Thanks @airfish. I am not sure how to align a table layout with a recyclerview and attach it dynamically from an activity.

              – Prashanth
              Nov 16 '18 at 22:23











            • I suppose you could use the same concept with your TableLayout embedded in the ScrollView instead of the RecyclerView above. The ScrollView would have the same properties as the RecyclerView and the TableLayout would stay the same.

              – rhpekarek
              Nov 16 '18 at 22:33











            • Please see edited xml

              – rhpekarek
              Nov 16 '18 at 22:39











            • Tried this - one problem - the scroll bar appears right next to the second column instead of being to the extreme right.

              – Prashanth
              Nov 16 '18 at 22:44











            • Try making the scrollview width match parent and table layout with layout gravity center in parent

              – rhpekarek
              Nov 16 '18 at 22:58



















            Thanks @airfish. I am not sure how to align a table layout with a recyclerview and attach it dynamically from an activity.

            – Prashanth
            Nov 16 '18 at 22:23





            Thanks @airfish. I am not sure how to align a table layout with a recyclerview and attach it dynamically from an activity.

            – Prashanth
            Nov 16 '18 at 22:23













            I suppose you could use the same concept with your TableLayout embedded in the ScrollView instead of the RecyclerView above. The ScrollView would have the same properties as the RecyclerView and the TableLayout would stay the same.

            – rhpekarek
            Nov 16 '18 at 22:33





            I suppose you could use the same concept with your TableLayout embedded in the ScrollView instead of the RecyclerView above. The ScrollView would have the same properties as the RecyclerView and the TableLayout would stay the same.

            – rhpekarek
            Nov 16 '18 at 22:33













            Please see edited xml

            – rhpekarek
            Nov 16 '18 at 22:39





            Please see edited xml

            – rhpekarek
            Nov 16 '18 at 22:39













            Tried this - one problem - the scroll bar appears right next to the second column instead of being to the extreme right.

            – Prashanth
            Nov 16 '18 at 22:44





            Tried this - one problem - the scroll bar appears right next to the second column instead of being to the extreme right.

            – Prashanth
            Nov 16 '18 at 22:44













            Try making the scrollview width match parent and table layout with layout gravity center in parent

            – rhpekarek
            Nov 16 '18 at 22:58







            Try making the scrollview width match parent and table layout with layout gravity center in parent

            – rhpekarek
            Nov 16 '18 at 22:58















            0














            Thanks @airfish and @Onik for your solutions. The solution from @airfish helped resolve my issue finally, after struggling with it for the past 2 days.



            Here is the final solution, for anyone interested in getting this to work.



            Final Layout File.



            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ScrollView
            android:id="@+id/sv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="45dp">
            <TableLayout android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center_horizontal"
            android:id="@+id/mainTable">
            </TableLayout>
            </ScrollView>
            <LinearLayout
            android:layout_marginTop="-45dp"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:layout_gravity="center">
            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Submit" />

            <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center_horizontal"
            android:text="Cancel" />
            </LinearLayout>
            </LinearLayout>


            MainActivity.java (Edit Line # 30 to increase/decrease the number of rows for testing)



            package com.example.scroll_ex;

            import android.app.Activity;
            import android.os.Bundle;
            import android.view.Gravity;
            import android.widget.TableLayout;
            import android.widget.TableRow;
            import android.widget.TextView;

            public class MainActivity extends Activity {
            @Override
            public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            TableLayout mainTable = findViewById(R.id.mainTable);

            mainTable.removeAllViews();

            int leftRowMargin = 0;
            int topRowMargin = 0;
            int rightRowMargin = 0;
            int bottomRowMargin = 0;
            int textSize = 0, smallTextSize = 0, mediumTextSize = 0;

            TableRow tr = new TableRow(this);
            TextView column1 = new TextView(this);
            TextView column2 = new TextView(this);

            for(int i=-1; i < 25; i++){

            tr = new TableRow(this);
            column1 = new TextView(this);
            column2 = new TextView(this);

            tr.setGravity(Gravity.CENTER);
            tr.setId(i+ 1);
            TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT);
            trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
            tr.setPadding(0,0,0,0);
            tr.setLayoutParams(trParams);

            if (i == -1) {
            column1.setText("Name");
            column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.MATCH_PARENT));
            column1.setPadding(5, 5, 1, 5);
            } else {
            column1.setText("Name #" + i);
            column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT));
            column1.setPadding(5, 0, 1, 5);
            }

            tr.addView(column1);


            if (i == -1) {
            column2.setText("Address");
            column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.MATCH_PARENT));
            column2.setPadding(5, 5, 1, 5);
            } else {
            column2.setText("Address " + i);
            column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT));
            column2.setPadding(5, 0, 1, 5);
            }

            tr.addView(column2);
            mainTable.addView(tr, trParams);
            }

            }
            }


            Scroll with buttons



            No Scroll






            share|improve this answer




























              0














              Thanks @airfish and @Onik for your solutions. The solution from @airfish helped resolve my issue finally, after struggling with it for the past 2 days.



              Here is the final solution, for anyone interested in getting this to work.



              Final Layout File.



              <?xml version="1.0" encoding="utf-8"?>
              <LinearLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

              <ScrollView
              android:id="@+id/sv"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:paddingBottom="45dp">
              <TableLayout android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:gravity="center_horizontal"
              android:id="@+id/mainTable">
              </TableLayout>
              </ScrollView>
              <LinearLayout
              android:layout_marginTop="-45dp"
              android:layout_width="wrap_content"
              android:layout_height="45dp"
              android:orientation="horizontal"
              android:layout_gravity="center">
              <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:text="Submit" />

              <Button
              android:id="@+id/button2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="10dp"
              android:layout_gravity="center_horizontal"
              android:text="Cancel" />
              </LinearLayout>
              </LinearLayout>


              MainActivity.java (Edit Line # 30 to increase/decrease the number of rows for testing)



              package com.example.scroll_ex;

              import android.app.Activity;
              import android.os.Bundle;
              import android.view.Gravity;
              import android.widget.TableLayout;
              import android.widget.TableRow;
              import android.widget.TextView;

              public class MainActivity extends Activity {
              @Override
              public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              TableLayout mainTable = findViewById(R.id.mainTable);

              mainTable.removeAllViews();

              int leftRowMargin = 0;
              int topRowMargin = 0;
              int rightRowMargin = 0;
              int bottomRowMargin = 0;
              int textSize = 0, smallTextSize = 0, mediumTextSize = 0;

              TableRow tr = new TableRow(this);
              TextView column1 = new TextView(this);
              TextView column2 = new TextView(this);

              for(int i=-1; i < 25; i++){

              tr = new TableRow(this);
              column1 = new TextView(this);
              column2 = new TextView(this);

              tr.setGravity(Gravity.CENTER);
              tr.setId(i+ 1);
              TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
              TableLayout.LayoutParams.WRAP_CONTENT);
              trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
              tr.setPadding(0,0,0,0);
              tr.setLayoutParams(trParams);

              if (i == -1) {
              column1.setText("Name");
              column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
              TableRow.LayoutParams.MATCH_PARENT));
              column1.setPadding(5, 5, 1, 5);
              } else {
              column1.setText("Name #" + i);
              column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
              TableRow.LayoutParams.WRAP_CONTENT));
              column1.setPadding(5, 0, 1, 5);
              }

              tr.addView(column1);


              if (i == -1) {
              column2.setText("Address");
              column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
              TableRow.LayoutParams.MATCH_PARENT));
              column2.setPadding(5, 5, 1, 5);
              } else {
              column2.setText("Address " + i);
              column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
              TableRow.LayoutParams.WRAP_CONTENT));
              column2.setPadding(5, 0, 1, 5);
              }

              tr.addView(column2);
              mainTable.addView(tr, trParams);
              }

              }
              }


              Scroll with buttons



              No Scroll






              share|improve this answer


























                0












                0








                0







                Thanks @airfish and @Onik for your solutions. The solution from @airfish helped resolve my issue finally, after struggling with it for the past 2 days.



                Here is the final solution, for anyone interested in getting this to work.



                Final Layout File.



                <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ScrollView
                android:id="@+id/sv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingBottom="45dp">
                <TableLayout android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:gravity="center_horizontal"
                android:id="@+id/mainTable">
                </TableLayout>
                </ScrollView>
                <LinearLayout
                android:layout_marginTop="-45dp"
                android:layout_width="wrap_content"
                android:layout_height="45dp"
                android:orientation="horizontal"
                android:layout_gravity="center">
                <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Submit" />

                <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_horizontal"
                android:text="Cancel" />
                </LinearLayout>
                </LinearLayout>


                MainActivity.java (Edit Line # 30 to increase/decrease the number of rows for testing)



                package com.example.scroll_ex;

                import android.app.Activity;
                import android.os.Bundle;
                import android.view.Gravity;
                import android.widget.TableLayout;
                import android.widget.TableRow;
                import android.widget.TextView;

                public class MainActivity extends Activity {
                @Override
                public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                TableLayout mainTable = findViewById(R.id.mainTable);

                mainTable.removeAllViews();

                int leftRowMargin = 0;
                int topRowMargin = 0;
                int rightRowMargin = 0;
                int bottomRowMargin = 0;
                int textSize = 0, smallTextSize = 0, mediumTextSize = 0;

                TableRow tr = new TableRow(this);
                TextView column1 = new TextView(this);
                TextView column2 = new TextView(this);

                for(int i=-1; i < 25; i++){

                tr = new TableRow(this);
                column1 = new TextView(this);
                column2 = new TextView(this);

                tr.setGravity(Gravity.CENTER);
                tr.setId(i+ 1);
                TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
                trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
                tr.setPadding(0,0,0,0);
                tr.setLayoutParams(trParams);

                if (i == -1) {
                column1.setText("Name");
                column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.MATCH_PARENT));
                column1.setPadding(5, 5, 1, 5);
                } else {
                column1.setText("Name #" + i);
                column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
                column1.setPadding(5, 0, 1, 5);
                }

                tr.addView(column1);


                if (i == -1) {
                column2.setText("Address");
                column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.MATCH_PARENT));
                column2.setPadding(5, 5, 1, 5);
                } else {
                column2.setText("Address " + i);
                column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
                column2.setPadding(5, 0, 1, 5);
                }

                tr.addView(column2);
                mainTable.addView(tr, trParams);
                }

                }
                }


                Scroll with buttons



                No Scroll






                share|improve this answer













                Thanks @airfish and @Onik for your solutions. The solution from @airfish helped resolve my issue finally, after struggling with it for the past 2 days.



                Here is the final solution, for anyone interested in getting this to work.



                Final Layout File.



                <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ScrollView
                android:id="@+id/sv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingBottom="45dp">
                <TableLayout android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:gravity="center_horizontal"
                android:id="@+id/mainTable">
                </TableLayout>
                </ScrollView>
                <LinearLayout
                android:layout_marginTop="-45dp"
                android:layout_width="wrap_content"
                android:layout_height="45dp"
                android:orientation="horizontal"
                android:layout_gravity="center">
                <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Submit" />

                <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_horizontal"
                android:text="Cancel" />
                </LinearLayout>
                </LinearLayout>


                MainActivity.java (Edit Line # 30 to increase/decrease the number of rows for testing)



                package com.example.scroll_ex;

                import android.app.Activity;
                import android.os.Bundle;
                import android.view.Gravity;
                import android.widget.TableLayout;
                import android.widget.TableRow;
                import android.widget.TextView;

                public class MainActivity extends Activity {
                @Override
                public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                TableLayout mainTable = findViewById(R.id.mainTable);

                mainTable.removeAllViews();

                int leftRowMargin = 0;
                int topRowMargin = 0;
                int rightRowMargin = 0;
                int bottomRowMargin = 0;
                int textSize = 0, smallTextSize = 0, mediumTextSize = 0;

                TableRow tr = new TableRow(this);
                TextView column1 = new TextView(this);
                TextView column2 = new TextView(this);

                for(int i=-1; i < 25; i++){

                tr = new TableRow(this);
                column1 = new TextView(this);
                column2 = new TextView(this);

                tr.setGravity(Gravity.CENTER);
                tr.setId(i+ 1);
                TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
                trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
                tr.setPadding(0,0,0,0);
                tr.setLayoutParams(trParams);

                if (i == -1) {
                column1.setText("Name");
                column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.MATCH_PARENT));
                column1.setPadding(5, 5, 1, 5);
                } else {
                column1.setText("Name #" + i);
                column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
                column1.setPadding(5, 0, 1, 5);
                }

                tr.addView(column1);


                if (i == -1) {
                column2.setText("Address");
                column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.MATCH_PARENT));
                column2.setPadding(5, 5, 1, 5);
                } else {
                column2.setText("Address " + i);
                column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
                column2.setPadding(5, 0, 1, 5);
                }

                tr.addView(column2);
                mainTable.addView(tr, trParams);
                }

                }
                }


                Scroll with buttons



                No Scroll







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 23:13









                PrashanthPrashanth

                1,08911018




                1,08911018






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345826%2fstatic-buttons-under-a-scrollview-with-a-dynamic-tablelayout-within-it%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine