Android ScrollView set height for displayed content

50,149

Solution 1

What you need is a maxHeight. But since views do not support it, you have to use a workaround.

Check out this answer:

https://stackoverflow.com/a/13811461/770467

Solution 2

With some help from this answer I managed to wrap up a very basic ScrollView component you can use for this case:

Create a custom class extending a ScrollView and do the following modifications:

public class MaxHeightScrollView extends ScrollView {

    private int maxHeight;

    public MaxHeightScrollView(Context context) {
        super(context);
    }

    public MaxHeightScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
            maxHeight = styledAttrs.getDimensionPixelSize(R.styleable.MaxHeightScrollView_maxHeight, 200); //200 is a default value
            styledAttrs.recycle();
        }
    }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
   }
}

And then one minor thing left is to declare your styleable in the attrs.xml file of your values folder(If you don't have one, just create an xml file with this name in the values folder of your project's res folder). Add the following lines there:

<declare-styleable name="MaxHeightScrollView">
    <attr name="maxHeight" format="dimension" />
</declare-styleable>

And use your new ScrollView as follows:

<com.yourpackage.MaxHeightScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:maxHeight="300dp">
</com.yourpackage.MaxHeightScrollView>

Credits go to whizzle for quickly wrapping this up!

Solution 3

My simple solution.
Set ScrollView heigh in xml:

android:layout_height="wrap_content"

Add this code to set Max Height to 200 if current measured Height is > 200

sv.measure(0, 0);
if (nsv.getMeasuredHeight() > 200) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                      ViewGroup.LayoutParams.MATCH_PARENT, 200);
        sv.setLayoutParams(lp);
}

ScrollView in my example is into LinearLayout, so I used LinearLayout.LayoutParams.

Solution 4

Add a layout around your ScrollView with fixed height

Share:
50,149
user2150445
Author by

user2150445

Updated on June 29, 2020

Comments

  • user2150445
    user2150445 almost 4 years

    I'm having a lot of trouble trying solve the issue of having a static height scrollable area within a layout. I have three long lists that need to be displayed on the same screen and it would be entirely impractical to display all of the entries sequentially because then if you want to skip a category you need to scroll past possibly hundreds of entries.

    Assume I have a scroll view with a Linear Layout inside of it, I want this to take up say, 250dp height on the screen at max and be independently scrollable once populated with more entries than can fit in 250dp's space.

    What I have now is:

    <ScrollView
        android:minWidth="25px"
        android:minHeight="150px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/XXXXXXX"
        android:scrollbars="vertical">
    
        <LinearLayout
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/XXXXXX" />
    </ScrollView>
    

    When populated, the scrollview and linearlayout just stretch as long as they needs to to fit the content and displays all of it instead of having a "window" of 250dp/px (any measurment would be nice) with the content scrollable within it.

    I'm new to the android platform, so perhaps the answer is obvious and I just didn't understand the language, but any help would be greatly appreciated! Thank you

    --- SOLVED: Put a linearLayout outside of the ScrollView with the height.

  • user2150445
    user2150445 almost 11 years
    I got the sizing part to work with your article, but now that area isn't scrolling. I've updated the question with the new code. Thank you so far!
  • Androiderson
    Androiderson almost 11 years
    That's strange, I've just run a sample and it scrolls fine, here's the layout: pastebin.com/raw.php?i=R45Z7wBi
  • android developer
    android developer about 10 years
    @ExceptionAl the pasteBin website says "This paste has been removed!"
  • RobertoAllende
    RobertoAllende almost 8 years
    Thank you very much, this is just what I was looking for.
  • Admin
    Admin over 4 years
    It works except that I dont know how to use the attribute in my xml.