Search code examples
androidscrollviewandroid-relativelayout

Scrollview inside Relativelayout is not scrolling properly in android


In my screen design, I have an Action Bar and Bottom Bar (which basically a Linearlayout). In mid of the screen, I have few textviews and 4-5 buttons. I want to add the scroll in the middle part of the screen. But it is not working.

My code is here:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

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

            <TextView
                android:id="@+id/toAddrdrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/fromAddrDrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/toAddrdrop"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/distanceToCoverDrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/fromAddrDrop"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/distanceToCoverDrop"
                android:orientation="vertical">

                <Button
                    android:id="@+id/drop_confirm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Drop Confirm" />

                <Button
                    android:id="@+id/sai_out_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Sai Out" />

                <Button
                    android:id="@+id/customer_in_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer In" />

                <Button
                    android:id="@+id/customer_out_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer Out" />

                <Button
                    android:id="@+id/customer_new_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer new" />

                <Button
                    android:id="@+id/customer_new_drop22"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer new 22" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/buttombardrop"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:background="#33B5E5"
        android:orientation="horizontal">

        <Button
            android:id="@+id/callButton_drop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@android:drawable/ic_menu_call"
            android:paddingTop="8dp"
            android:text="Call"
            android:textColor="#FFFFFF" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#ffffff" />

        <Button
            android:id="@+id/mapButton_drop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@android:drawable/ic_menu_mapmode"
            android:paddingTop="8dp"
            android:text="Map"
            android:textColor="#FFFFFF" />

    </LinearLayout >

</RelativeLayout>

I have observed that, I am able to scroll a bit till 4 buttons. How can I fix the scrollview. Please help!!


Solution

  • You should beware that RelativeLayout behaves unexpected many times. so you should use LinearLayout.

    Just replace your code by

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ScrollView
    
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/toAddrdrop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="20dp"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/fromAddrDrop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/toAddrdrop"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/distanceToCoverDrop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/fromAddrDrop"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
    
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/distanceToCoverDrop"
                    android:orientation="vertical">
    
                    <Button
                        android:id="@+id/drop_confirm"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="40dp"
                        android:background="@drawable/button_active"
                        android:text="Drop Confirm" />
    
                    <Button
                        android:id="@+id/sai_out_drop"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="40dp"
                        android:background="@drawable/button_active"
                        android:text="Sai Out" />
    
                    <Button
                        android:id="@+id/customer_in_drop"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="40dp"
                        android:background="@drawable/button_active"
                        android:text="Customer In" />
    
                    <Button
                        android:id="@+id/customer_out_drop"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="40dp"
                        android:background="@drawable/button_active"
                        android:text="Customer Out" />
    
                    <Button
                        android:id="@+id/customer_new_drop"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="40dp"
                        android:background="@drawable/button_active"
                        android:text="Customer new" />
    
                    <Button
                        android:id="@+id/customer_new_drop22"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="40dp"
                        android:background="@drawable/button_active"
                        android:text="Customer new 22" />
                </LinearLayout>
    
            </LinearLayout>
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/buttombardrop"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:background="#33B5E5"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/callButton_drop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:drawableTop="@android:drawable/ic_menu_call"
                android:paddingTop="8dp"
                android:text="Call"
                android:textColor="#FFFFFF" />
    
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#ffffff" />
    
            <Button
                android:id="@+id/mapButton_drop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:drawableTop="@android:drawable/ic_menu_mapmode"
                android:paddingTop="8dp"
                android:text="Map"
                android:textColor="#FFFFFF" />
    
        </LinearLayout >
    
    </LinearLayout>