Search code examples
androidandroid-layoutandroid-linearlayouttextviewandroid-scrollview

How do I get the linearlayout to take up the whole space of the scrollview?


I have a scrollview sView that takes up my whole screen that will hold a linearlayout newLL that will also take up the whole screen that will hold various numbers of textviews. I want the textviews to show up starting from the bottom of the screen and then become scrollable when they fill up to the top. My problem is that when I add background colors to see where layouts are, the linearlayout newLL appears to wrap contents at the top of the screen and does not fill the whole screen. This causes the textviews to fill from top down instead of bottom up.

This is my relevant code:

ScrollView sView = (ScrollView) findViewById(R.id.scrollView2)
sView.removeAllViews();

LinearLayout newLL = new LinearLayout(this);
newLL.setOrientation(LinearLayout.VERTICAL);

ScrollView.LayoutParams sparams = new ScrollView.LayoutParams(
           ScrollView.LayoutParams.MATCH_PARENT,
           ScrollView.LayoutParams.MATCH_PARENT
);
newLL.setLayoutParams(sparams);
sView.addView(newLL);

Note: When I add in the textviews, I set their gravity to bottom but since the linearlayout wraps and is at the top, it doesn't do anything. I suspect something wrong with my sparams.


Solution

  • You need to set android:fillViewport property to your ScrollView. You can either do that in layout xml by putting:

    android:fillViewport="true"
    

    within the scrollview description. Or,

    ScrollView sView = (ScrollView) findViewById(R.id.scrollView2)
    sView.removeAllViews();
    sView.setFillViewport(true);