0

I'm trying to make a list of checkboxes dynamically and I need these checkboxes to be in a ScrollView. I've managed to add the checkboxes dynamically, but it won't scroll even when there are a lot of checkboxes. This is the ScrollView:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

And this is the code that adds the checkboxes dynamically:

// sets up the payee checkboxes
LinearLayout payersLinearLayout = (LinearLayout) findViewById(R.id.payersLinearLayout);

participants.add("Test 1");
participants.add("Test 2");
participants.add("Test 3");
participants.add("Test 4");
participants.add("Test 5");
participants.add("Test 6");
participants.add("Test 7");
participants.add("Test 8");

int participantsSize = participants.size();

for (int i = 0; i < participantsSize; i++) {
    LinearLayout payerLinearLayout = new LinearLayout(this);

    payerLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            payerLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT,
        1.0f
    );
    CheckBox checkBox = new CheckBox(this);
    checkBox.setId(i);
    checkBox.setText(participants.get(i));
    checkBox.setLayoutParams(param);

    EditText editText = new EditText(this);
    editText.setText("0.00");
    editText.setLayoutParams(param);

    payerLinearLayout.addView(checkBox);
    payerLinearLayout.addView(editText);

    payersLinearLayout.addView(payerLinearLayout);
}

I'm not sure what the problem is and why it won't scroll. I'm not sure if it's relevant, but the ScrollView itself is in a Linear Layout, which is also in a Linear Layout, which is in an App Bar Layout. Could anybody help? Any help would be appreciated!

dasyurus
  • 53
  • 1
  • 4
  • Why your CheckBox and Edittext both have width as `MATCH_PARENT` while the layout orientation is Horizontal ? can u add the expected output image – ADM Dec 07 '20 at 07:39
  • @ADM Ah I copied and pasted that from a StackOverflow answer... I was looking for how to set the layout_weight dynamically. Does it affect the ScrollView problem? As for how I want it to look, it's similar to the checkbox list at https://www.tricount.com/photos/faq/17_2_money_transfer_off.png but there's a TextView to its left with the label "Paid For". – dasyurus Dec 07 '20 at 08:28
  • You should use `RecyclerView` for this adding views inside Layout can get messy afterwards. If data is not much then you can set height of `RecyclerView` equal to items height so it will not recycle the views . u can also use [This](https://stackoverflow.com/questions/36313079/i-want-my-recyclerview-to-not-recycle-some-items/36313437). – ADM Dec 07 '20 at 08:35
  • @ADM RecyclerViews are more complex to implement than a ScrollView and so I decided to go with a ScrollView, but if this keeps on not working then I'll implement a RecyclerView.. – dasyurus Dec 07 '20 at 08:37
  • `RecyclerView` can be a bit complex if you are new to it . But its the Core stuff and its quite easy to use and much more flexible . give it a try . u can not skip it either way so why not start it now . – ADM Dec 07 '20 at 09:11
  • @ADM I implemented the RecyclerView, but the scrolling is also not working... – dasyurus Dec 07 '20 at 14:35

1 Answers1

0

For anyone else encountering the problem, I've finally figured it out. The problem was the App Bar Layout. I tried replacing the ScrollView with a RecyclerView, but it was exactly the same. Scrolling didn't work. It worked when I removed the App Bar Layout. It seems RecyclerViews don't work inside App Bar Layouts. I hope this helps somebody out there!

dasyurus
  • 53
  • 1
  • 4
  • That's not right . You need to use `NestedScrollView` instead of `ScrollView` to make it scroll See [This](https://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working). – ADM Dec 08 '20 at 04:35
  • @ADM Mine is working now though, it's a ScrollView (I replaced the RecyclerView again due to an unrelated issue) inside a Linear Layout inside a Linear Layout, do I need to replace it with a NestedScrollView? – dasyurus Dec 08 '20 at 08:00