Display name Display name - 1 year ago 93
Android Question

Layout changes when dynamic activity is started

I have a layout which allows for a dynamic EditText to be created - this works fine but it pushes up the static EditTexts which is not what I want.

I am using a scrollview so in theory the static EditText should anchor in place and when the dynamic EditTexts are added, the user can just scroll down.

Instead, the 3 static EditTexts I have start moving closer and closer together as more dynamic EditTexts are added. The spacing in the dynamic EditTexts are spacing fine as they are all part of the same array, but I can't seem to see whats happening with the static EditTexts.

Any help would be appreciated.

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/facebookBlue"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.test.practise.AddTeamMembers">


<android.support.design.widget.TextInputEditText
android:id="@+id/tv_teamNames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/teamName"
android:textColor="@android:color/background_light"
android:textColorLink="@android:color/background_light"
android:textSize="30sp"
android:textStyle="bold" />


<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:orientation="vertical"
android:weightSum="1">

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.47"
android:gravity="center"
android:text="Enter Player Names Below!"
android:textColor="@android:color/background_light"
android:textSize="24sp" />

</FrameLayout >


<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:orientation="vertical"
android:weightSum="1">

<EditText

android:id="@+id/et_team_name1"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:layout_weight="1"
android:background="@android:color/background_light"
android:ems="10"
android:hint="@string/PlayerName"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="90dp"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp"
/>

</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:orientation="vertical"
android:weightSum="1">

<EditText
android:id="@+id/et_team_name2"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:layout_weight="1"
android:background="@android:color/background_light"
android:ems="10"
android:hint="@string/PlayerName"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="90dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp"
/>

</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_weight="0.10"
android:orientation="vertical"
android:weightSum="1">

<EditText
android:id="@+id/et_team_name3"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:background="@android:color/background_light"
android:ems="10"
android:hint="@string/PlayerName"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="90dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp"
android:layout_weight="1"
/>

</FrameLayout>

<LinearLayout
android:id="@+id/editTextGroupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"></LinearLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:orientation="vertical"
android:weightSum="1">

<TextView
android:id="@+id/tv_add_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="+ Add Name"
android:textColor="@android:color/background_light"
android:textSize="16dp" />

</FrameLayout >


<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">

<Button
android:id="@+id/btn_submit_team"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/facebookBlue"
android:gravity="center"
android:text="Ready to join!"
android:textColor="@android:color/background_light" />

</FrameLayout >


</LinearLayout>

</ScrollView>


Which results in this

Before Dynamic EditText is added

After Dynamic EditText is added

Answer Source

It is hard to say without seeing all of your code, but I believe your problem stems from the use of android:fillViewport on your ScrollView and android:layout_weight on your LinearLayout's children.

Your ScrollView's child, the LinearLayout, has wrap_content height. However, due to the fillViewport attribute on the ScrollView, your LinearLayout will be stretched to fill the screen. This means there is going to be some "extra" height.

The layout_weight attributes divide up that extra height, and give some of it to each child.

As you dynamically add EditTexts to your view, the amount of height that is "extra" is reduced (until there is no extra height). This has the visual effect of pushing your original views closer and closer together.

The solution will depend on exactly what you want to achieve. I suspect that the answer is that you should remove the layout_weight attributes from your views, and instead use margins to separate them visually.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download