Layout Weights Do Not Work Inside a Scrollview

Layout Weights do not work inside a ScrollView

I have faced this problem before. Just use android:fillViewport="true" in your ScrollView and it will fill up the screen.

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

XML Layout weight not working in a ScrollView on android API23

One should understand how dimensions are calculated.
Let's look one by one.

1) ScrollView is fill_parent/fill_parent (actually one can change this to match_parent/match_parent as fill_parent is deprecated as far as I remember, but it doesn't matter in this case).
So, ScrollView is shown fits whole screen (width and height).

2) LinearLayout inside ScrollView is match_parent/wrap_content. And that is correct. One should use wrap_content for views that inside ScrollView.
The reason why - is that ScrollView is used to wrap long views with usually unknown height (like lists). Imagine here match_parent as height - height of LinearLayout would be calculated based on height of scroll view, which is whole screen height, and LinearLayout height can be greater that screen height. Just not logical. But everything is OK till now. Move on.

3) Items inside LinearLayout. You want them to be 1/10 and 9/10 of height of parent layout. Also, you set height for inner items to 0 to show that you want height to be calculated from weights.
And here you got a cycle of dependencies. LinearLayout height is wrap_content and depends on sum of heights of inner elements.
And inner elements' height is calculated from parent LinearLayout height.

Additionally, you want ImageView to be 1/10 of the LinearLayout height. But actually seems like you want ImageView to be 1/10 of the screen's height, as LinearLayout height can be greater than screen's height (and looks like this is what you get on your device).

You say, that everything is OK in preview in Android Studio.
To demonstrate that not everything is OK try this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:fillViewport="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_weight="1"
android:adjustViewBounds="true"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:id="@+id/image"
android:scaleType="fitCenter"
android:src="@drawable/bjj2" />
<LinearLayout
android:layout_weight="10"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp">
<View
android:layout_width="match_parent"
android:layout_height="1000dp"/>

</LinearLayout>
</LinearLayout>

</ScrollView>

Just put some big (bigger than screen's height) view inside your inner-inner LinearLayout and you will see that in preview your image is bigger than 1/10 of screen's height. But it is 1/10 of LinearLayout's height.

How to solve your issue?
I think the only way is to calculate 1/10 of the screen's height and resize bitmap with image you want to have and set that bitmap to ImageView.
Or any other way to set exact height to ImageView.

Or review your layout and create something different.

Android : LinearLayout with weight inside a ScrollView

I solved it by adding a parent Linear Layout (and a main relativeLayout) :

    ScrollView (Vertical)
LineaLayout
LinearLayout (Vertical)
ImageView weight= 0.5
Whatever weight= 0.1
Whatever weight= 0.2
Whatever weight= 0.2

And setting programmatically the height of the child LinearLayout to the height of the screen with childLinearLayout.getLayoutParams().height

ScrollView with weight not working for me

Experimented your code and found the issue.

From your code, it does seems like what you want is a Horizontal Scroll. But you are using normal ScrollView which is by default Vertical Scroll. Consider changing ScrollView to HorizontalScrollView.

i.e. from

<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_weight="1"
android:layout_marginRight="8dp">

to

<HorizontalScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_weight="1"
android:layout_marginRight="8dp">

Do change on both your scroll view. Cheers!

layout_weight does not work when the layout inside a ScrollView is larger than the ScrollView

The weights are working as they were designed. Maybe not the way you want them to "work", but saying they are "not working" is inaccurate. Think about your 3rd case for a second, what size in pixels would you expect the TextViews to be? Weights are a percentage of the available space (not total size of the parent). The scrollview and linearlayout are, at a minimum, the size of your screen, otherwise the sum of all the children views. So if your children views exceed the size of the screen (the ones without weights), how much space do you expect to be allocated for the remaining views (the ones with 0dp and weights)? The point is that amount of space is arbitrary, android will not try to assume how much space you want, because it has no possible way of knowing.

TL;DR - set a minHeight (in dp) on your textviews to handle the case where you have too many buttons.



Related Topics



Leave a reply



Submit