Search code examples
javaandroidxmlandroid-scrollview

My scrollview in a linear layout is pushing up other widgets above it


So my button and edit text widgets keep getting pushed upwards when I dynamically add text views to my scroll view. So the scrollview's height is also being bumped up. How do I just make sure the scroll view is fixed, I've looked at similar questions and tried implementing it but not working.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:windowSoftInputMode="stateVisible|adjustPan"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/name"
        android:hint="Enter name"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"/>

    <EditText
        android:id="@+id/phone_num"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:hint="Enter Phone Number"/>

    <Button
        android:id="@+id/submit"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:text="@string/submit_contact"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:isScrollContainer="false"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>

    </ScrollView>

</LinearLayout>

Solution

  • Try to set all heights to 0dp and give the wheightSum to the parent view. To have a fixed height of the button surround it with a LinearLayout.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:windowSoftInputMode="stateVisible|adjustPan"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2.75"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <EditText
            android:id="@+id/name"
            android:hint="Enter name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25"/>
    
        <EditText
            android:id="@+id/phone_num"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25"
            android:hint="Enter Phone Number"/>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25"
            android:orientation="vertical">
    
            <Button
                android:id="@+id/submit"
                android:layout_width="150dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:text="test"/>
            
        </LinearLayout>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:isScrollContainer="false"
            android:fillViewport="true">
    
            <LinearLayout
                android:id="@+id/layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            </LinearLayout>
    
        </ScrollView>
    
    </LinearLayout>

    Try it out, If it dont work you may post what you have tried alredy and the code how you ad the textViews dynamicly. Good luck