ScrollView not scrollable, due to programmatically inserted data?
-
07-07-2021 - |
Question
I've seen non-scrollable ScrollView's a few times on stackoverflow.com, but none of the proposed solutions work for me. Ihope someone can help.
I have a ScrollView containing a TableLayout. The TableLayout is empty, but I programmatically insert rows and columns. I am guessing that I need to tell the ScrollView that the TableLayout has changed/updated, and ScrollView needs to recalculate if it needs to enable scrolling.
I tried to invalidate both the ScrollView and the TableLayout, I tried requestLayout(), but nothing seems to do anything. What am I missing?
Here is my layout XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="horizontal|vertical" >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/maintable" >
</TableLayout>
</ScrollView>
And here is my Java code. Basicly, this code adds a row per player and 25 buttons next to each player. The buttons are there, but they are out of the screen boundaries.
// Get the TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
// Go through each item in the array
for (int player = 0; player < players.length; player++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+player);
tr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to add the player name
TextView labelTV = new TextView(this);
labelTV.setId(200+player);
labelTV.setText(players[player]);
//labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// numShots = 25
for (int shot = 0; shot < numShots; shot++)
{
Button shotButton = new Button(this);
shotButton.setId((player * 100) + shot);
shotButton.setText(Integer.toString(shot));
tr.addView(shotButton);
}
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Add the TableRow to the TableLayout
//};
}
Solution
I think you're needing a horizontal scroll? ScrollView only does vertical scrolling, you'll need to have a look at HorizontalScrollView.