2

I'm adding Views dinamically to a Scrollview (LinearLayout) and It won´t show everything. For example, if I add 10 Views (TextView and EditView) it'll show like 4 and if I add more, like 200, It'll reach 'til the 131 but I can see like the beginning of the next View Like this:

This is the XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".FirstActivity"
tools:layout_editor_absoluteY="25dp">

<TextView
    android:id="@+id/textView"
    android:layout_width="95dp"
    android:layout_height="36dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ScrollView
    android:id="@+id/scrollView3"
    android:layout_width="362dp"
    android:layout_height="0dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    app:layout_constraintBottom_toTopOf="@+id/button4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.47"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    android:paddingBottom="10dp">

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

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

And this is the java:

public class FirstActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    LinearLayout layout = findViewById(R.id.layout);
    int i = 1;
    int a = 1;
    int noc = 500;
    int heightTV = 100;
    int heightET = 10;
    int widthTV = 100;
    int widthET = 200;
    do{
        TextView textView = new TextView(this);
        textView.setId(i);
        String number = Integer.toString(a)+".-";
        textView.setY(heightTV);
        textView.setX(widthTV);
        textView.setText(number);
        layout.addView(textView);
        EditText editText = new EditText(this);
        editText.setId(i);
        editText.setHint("Example: 567-ZTY");
        editText.setY(heightET);
        editText.setX(widthET);
        editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout.addView(editText);
        i++;
        a++;
        heightTV = heightTV + 100;
        heightET = heightET + 100;
    } while (i<=noc);
}

Everything else seems to work fine. Any ideas?

Stef Silva
  • 35
  • 4

2 Answers2

0

add this attribute to your scrollView:

     android:fillViewport="true"

also your height is "0dp". I realize why you are doing this, but definitely check the view that it is constrained to and make sure that there is enough room to show what you are trying to display.

Paris B. G.
  • 374
  • 1
  • 3
  • 14
0

So I don't know why, but it seems that if I add something outside of the loop once has finished working, where the scrollview normally stops, the missing content is shown.

The new code is:

for (int i = 1; i<=noc; ++i){
        TextView textView = new TextView(this);
        textView.setId(i);
        String number = Integer.toString(a)+".-";
        textView.setY(heightTV);
        textView.setX(widthTV);
        textView.setText(number);
        layout.addView(textView);
        EditText editText = new EditText(this);
        editText.setId(i);
        editText.setHint("Example: 567-ZTY");
        editText.setY(heightET);
        editText.setX(widthET);
        editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout.addView(editText);
        a++;
        heightTV = heightTV + 100;
        heightET = heightET + 100;
        Button button = findViewById(R.id.button4);
        button.setText(Integer.toString(i));
    }
    heightTV = heightTV + 100;
    TextView textV = new TextView(this);
    textV.setHeight(heightTV);
    textV.setText("");
    layout.addView(textV);
Stef Silva
  • 35
  • 4