Search code examples
androidandroid-layoutandroid-xmlandroid-scrollview

ScrollView not scrolling down completely in XML in Android


I have a XML code which displays the Cricket scorecard of a team. This is the my scorecard.xml code where I am using NonScrollableList under Linear Layout in Scrollview:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:fillViewport="true"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/windowBackground"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="50.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@drawable/scorecard_uperlayout">

            <TextView
                android:id="@+id/Batteam_description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text="Aus"
                android:textColor="@color/textColorPrimary"
                android:textSize="18.0sp" />
        </RelativeLayout>

        <LinearLayout
            android:id="@id/tabllayout"
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="11.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5.5">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Batsman"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="B"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4s"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="6s"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="2.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="SR"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip">

            <com.example.live_score.activity.NonScrollableList
                android:id="@+id/list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/layoutBackground"
            android:visibility="visible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text=" Extras"
                android:textColor="@color/textcolor"
                android:textSize="16.0sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/layoutBackground"
            android:visibility="visible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text="Yet to Bat"
                android:textColor="@color/textcolor"
                android:textSize="16.0sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tabllayout_bowl"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="11.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5.3">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Bowler"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="O"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="M"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="W"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Wd"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Nb"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:visibility="visible">

            <com.example.live_score.activity.NonScrollableList
                android:id="@+id/list_bowlings"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="9.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="6.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Fall of Wickets"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="W"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="O"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>
</ScrollView>

Below is the NonScrollableList activity code:

public class NonScrollableList extends ListView {

    public NonScrollableList(Context context) {
        super(context);
    }
    public NonScrollableList(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NonScrollableList(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

Below is onCreateView for the above code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.scorecard, container, false);
        listView = (NonScrollableList) rootView.findViewById(R.id.list);
        list_bowlings = (NonScrollableList) rootView.findViewById(R.id.list_bowlings);
}

But the problem I am not able to scroll down completely. Where do I need to add more height to fix the scroll ? Can anyone help ? Thanks in advance


Solution

  • just use android:nestedScrollingEnabled="true" in ListView