Manish Kumar Sharma
Manish Kumar Sharma

Reputation: 13432

Dynamically adding child views to a ScrollView

I have a ScrollView which has a LinearLayout as a child and which contains all the other views that I shall be scrolling like this:

<?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="wrap_content"
        android:layout_weight="1">
        <LinearLayout
            android:id="@+id/profile_fields_container"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/account_person_name_label" />

            <EditText
                android:id="@+id/person_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="150dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/account_person_emailId_label" />
            <EditText
                android:id="@+id/person_emailId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:minWidth="150dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/account_person_phoneNo_label" />
            <EditText
                android:id="@+id/person_phoneNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:maxLength="10"
                android:minWidth="150dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/account_person_password_label" />
            <EditText
                android:id="@+id/password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:minWidth="150dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/account_person_retypePassword_label" />
            <EditText
                android:id="@+id/retype_password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:minWidth="150dp"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="20dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/learner_tutor_status_label" />

                <android.support.v7.widget.AppCompatSpinner
                    android:id="@+id/learner_tutor_status_spinner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/create_account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/create_account_string"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_weight="0"/>

</LinearLayout>

Now, when I try to add another view inside the LinearLayout(which is inside the ScollView) at the end of it, dynamically, it does get added but half of it gets hidden underneath the Button(see the button with id @+id/create_account) and I am not able to scroll to it. It means that it doesn't get added to the ScrollView effectively.

This "other" view that I added dynamically is as follows:

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

    <!--Type of Tutor-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/type_of_tutor_label"
        android:textStyle="italic"/>
    <com.learncity.util.MultiSpinner
        android:id="@+id/type_of_tutor_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Professor, Undergraduate, Freelancer..."/>

    <!--Subjects/Disciplines-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/disciplines_label"
        android:textStyle="italic"
        android:layout_marginTop="10dp"/>
    <com.learncity.util.MultiSpinner
        android:id="@+id/subjects_taught_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Physics, Maths, Computer Science..."/>

</LinearLayout>

Does anyone know how to make the dynamically added view as effectively "scrollable" ?

EDIT 1:

In case if anyone wants to know how this conditional view is being added dynamically, here is the edited version of the code:

void showConditionalTutorUI() {
        // Initialize the first time
        if(typeOfTutorMultiSpinner == null && subjectsICanTeachMultiSpinner == null){
            if(profileFieldsContainer == null){
                profileFieldsContainer = (ViewGroup)rootView.findViewById(R.id.profile_fields_container);
            }
            if(rootTutorConditionalLayout == null){
                rootTutorConditionalLayout = layoutInflater.inflate(
                        R.layout.layout_conditional_tutor_ui,
                        profileFieldsContainer,
                        false);
            }
            // Add the inflated layout to the last of the container
            profileFieldsContainer.addView(rootTutorConditionalLayout, profileFieldsContainer.getChildCount());

        isConditionalTutorUIVisible = true;
    }

EDIT 2:

Okay, here is the LayoutInspector's view of it:(The view that I am hovering over in the hierarchy(highlighted by a red border) is the one not being seen.)

enter image description here

Upvotes: 2

Views: 1922

Answers (1)

Jay Rathod
Jay Rathod

Reputation: 11245

Try to add android:fillViewport="true" and android:scrollbars="vertical" to your Scroll View.

  <ScrollView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:fillViewport="true"
     android:scrollbars="vertical"
     android:layout_weight="1"/>

Upvotes: 2

Related Questions