With some help from this answer, I managed to wrap a very simple ScrollView component that you can use for this case:
Create your own class that extends ScrollView and make the following changes:
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);
And one more small thing is to declare your style 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 in the res folder of your project). 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>
Loans go to Whizzle to quickly wrap it up!
source share