Android ScrollView screen height for displayed content

I have a lot of problems trying to solve the problem of having a scrollable height area in the layout. I have three long lists that need to be displayed on one screen, and it would be completely impractical to display all the entries sequentially, because if you want to skip a category, you need to scroll past, possibly hundreds of entries.

Suppose I have a scroll with a linear layout inside it, I want it to say, the height is 250dp on the screen at max and it scrolls independently when filled with more records than it can fit in 250dp space.

I now have:

<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 filled, scrollview and linearlayout simply stretch as long as they need to fit the content and display all of it, instead of having a 250dp / px “window” (any dimension would be nice) with the content scrollable inside it.

I am new to the Android platform, so perhaps the answer is obvious, and I just did not understand the language, but any help would be greatly appreciated! thanks

--- SOLVED: Place a linear output outside the ScrollView with height.

+11
source share
5 answers

You need maxHeight . But since views do not support it, you should use a workaround.

Check this answer:

fooobar.com/questions/44903 / ...

+3
source

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); //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 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!

+17
source

My simple solution.
Set ScrollView heigh to xml:

 android:layout_height="wrap_content" 

Add this code to set the maximum height to 200 if the 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 in LinearLayout , so I used LinearLayout.LayoutParams .

+2
source

If I'm not mistaken, the height of the ViewGroup (the height of the LinearLayout in your case), that is, the (only) child inside the ScrollView, is always interpreted as wrap_content, as this content may be greater than the height of the ScrollView (hence the scroll bar).

This also means that if the content is smaller, the contents of the ScrollView (child) may not necessarily stretch to fill the screen.

To visually help you fix this, we should see a screenshot of your problem.

Perhaps setting android:fillViewport="true" in ScrollView fix your problem:

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> 
+2
source

Add a layout around your fixed-height scrollview

0
source

Source: https://habr.com/ru/post/949578/


All Articles