-1

In the code below, the total weight in the parent linear layout is less than the total weight of the its children.So I expect the scroll to work.The first children covers the entire screen.The other child is below the screen, but no scroll.

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".ActivityHome">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:orientation="vertical"
        android:weightSum="4">

        <!--First children-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="13"
            android:orientation="vertical">

        </LinearLayout>

        <!--Second children-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

        </LinearLayout>
    </LinearLayout>
</ScrollView>

I just have to work with layout_height.How can I do that?

1 Answers1

1

The Problem here is android:weightSum="4" . This is clearly opposite What ScrollView is build for . Remove Weight from layout If View goes out of the screen then it will automatically scroll . Try this for instance :-

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".ActivityHome">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:orientation="vertical"
        >

        <!--First children-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@android:color/black"
            android:orientation="vertical">

        </LinearLayout>

        <!--Second children-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:background="@android:color/holo_red_light"
            android:orientation="vertical">

        </LinearLayout>
    </LinearLayout>
</ScrollView>

See This Thread if you want to use android:fillViewport.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • How can I do this without using exact dimensions?(without layout_height="300dp" and layout_height="400dp") –  Mar 13 '19 at 13:08
  • The exact dimentions i just put for example . As i said if Child views goes out of screen the it will scroll automatically . Whats your case ? Edit your question with proper requirement . – ADM Mar 13 '19 at 13:11
  • Even if you got it working the way you're envisioning it in your head, I still think the exact dimension approach using dp would be better. Unless you have some weirdly specific use case. I spent a long time on my first app trying to do stuff like this before I realized that I should just let a ScrollView do its thing and scroll. – Gavin Wright Mar 13 '19 at 13:13