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;
}
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):
- If there are few rows, there shouldnt be a scroll view and the table should appear with the buttons just below the table.
- 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>
android android-layout scrollview
add a comment |
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):
- If there are few rows, there shouldnt be a scroll view and the table should appear with the buttons just below the table.
- 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>
android android-layout scrollview
add a comment |
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):
- If there are few rows, there shouldnt be a scroll view and the table should appear with the buttons just below the table.
- 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>
android android-layout scrollview
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):
- If there are few rows, there shouldnt be a scroll view and the table should appear with the buttons just below the table.
- 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>
android android-layout scrollview
android android-layout scrollview
edited Nov 16 '18 at 22:17
Prashanth
asked Nov 16 '18 at 21:43
PrashanthPrashanth
1,08911018
1,08911018
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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>
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 yourTableLayout
embedded in theScrollView
instead of theRecyclerView
above. TheScrollView
would have the same properties as theRecyclerView
and theTableLayout
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
|
show 3 more comments
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);
}
}
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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 yourTableLayout
embedded in theScrollView
instead of theRecyclerView
above. TheScrollView
would have the same properties as theRecyclerView
and theTableLayout
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
|
show 3 more comments
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>
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 yourTableLayout
embedded in theScrollView
instead of theRecyclerView
above. TheScrollView
would have the same properties as theRecyclerView
and theTableLayout
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
|
show 3 more comments
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>
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>
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 yourTableLayout
embedded in theScrollView
instead of theRecyclerView
above. TheScrollView
would have the same properties as theRecyclerView
and theTableLayout
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
|
show 3 more comments
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 yourTableLayout
embedded in theScrollView
instead of theRecyclerView
above. TheScrollView
would have the same properties as theRecyclerView
and theTableLayout
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
|
show 3 more comments
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);
}
}
}
add a comment |
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);
}
}
}
add a comment |
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);
}
}
}
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);
}
}
}
answered Nov 16 '18 at 23:13
PrashanthPrashanth
1,08911018
1,08911018
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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