Azizur Rehman
Azizur Rehman

Reputation: 2133

ScrollView not working in LinearLayout

I am trying to place a linearLayout inside a ScrollView but it isn't working.

Here is my code,

   <?xml version="1.0" encoding="utf-8"?>


<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:scrollbars="vertical"
android:background="@color/listChild"
android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="40dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:layout_gravity="center_horizontal"
    android:id="@+id/content_layout">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:weightSum="5"
        android:gravity="center">

        <LinearLayout
            android:layout_width="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="5dp">


        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@mipmap/ic_launcher"
            android:id="@+id/alphabet_icon"
            android:layout_weight="1" />


        </LinearLayout>


        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/word_text"
            android:layout_weight="3"
            android:gravity="center" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="5dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/share_button"
                android:id="@+id/share_button"
                android:layout_weight="1"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp" />

            <ImageView
            android:layout_width="match_parent"
            android:src="@drawable/save_new_icon"
            android:layout_weight="1"
            android:id="@+id/save_button"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_height="match_parent" />


        </LinearLayout>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_marginTop="20dp"
        android:weightSum="5"
        android:gravity="center"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:weightSum="5"
            android:layout_height="wrap_content">


        <TextView
            android:text="Pronunciation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />


            <ImageView
                android:src="@drawable/speak_icon_2"
                android:layout_weight="4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/speak_button"
                android:scaleType="fitCenter" />


        </LinearLayout>


        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:padding="10dp"
            android:id="@+id/pronounciation_text" />




    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_marginTop="20dp"
        android:weightSum="5"
        android:gravity="center"
        android:orientation="vertical">


        <TextView
            android:text="Type of Word"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView4"
            android:layout_weight="3"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />

        <TextView
            android:text="type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:padding="10dp"
            android:id="@+id/type_text" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:layout_marginTop="20dp"
        android:weightSum="5"
        android:gravity="center"
        android:orientation="vertical">


        <TextView
            android:text="Hindi Meaning"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />

        <TextView
            android:text="textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:padding="10dp"
            android:id="@+id/hindi_text" />


    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:layout_marginTop="20dp"
        android:weightSum="5"
        android:gravity="center"
        android:orientation="vertical">


        <TextView
            android:text="English Meaning"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView5"
            android:layout_weight="3"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />

        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:padding="10dp"
            android:id="@+id/english_meaning" />


    </LinearLayout>



</LinearLayout>

</ScrollView>

Also, i am using this layout in a fragment in viewPager. And the problem is that the layout is not scrolling.

The layout should look like this,

enter image description here

Can someone figure out what else am I missing or doing wrong. And If someone knows a better way to scroll a layout, please tell...

Upvotes: 0

Views: 1990

Answers (2)

Muhaiminur Rahman
Muhaiminur Rahman

Reputation: 3152

In my case, I just use this

android:fillViewport="true"

inside scrollview tag.. this works for me For example

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2">

Upvotes: 1

code_mc
code_mc

Reputation: 171

A ScrollView is an "infinite" container. It can only know how far you can scroll if it can properly calculate the height of its contents.

Currently you have your LinearLayout height set to match_parent. This will result in the LinearLayout never growing past the height of the ScrollView and therefor clipping all the child views.

This is a very long explanation for a very simple solution: set the layout_height of the LinearLayout to wrap_content.

Upvotes: 0

Related Questions