How can I make dynamically made buttons scrollable in Scrollview?

Cosmin-Adrian Bîrjoveanu :

I made buttons dynamically, to be aligned vertically after a normal button (made in xml), in a linear layout, which itself is the child of a Scrollview, but the Scrollview won't work, it treats said buttons as if they weren't part of the linear layout

I have tried adding buttons in xml in the linear layout, and those worked just fine. I also tried adding android:fillViewPort and setting it to true inside the ScrollView, and I tried adding an empty View to the bottom of the ScrollView, but it treated the start of the ScrollView as if it was the bottom, as if the buttons dynamically made weren't a part of it.

public class ShopActivity extends AppCompatActivity {

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

        LinearLayout linearLayout = (LinearLayout)  
        findViewById(R.id.LinearLayoutu);
        linearLayout.setGravity(Gravity.TOP);

        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearParams.gravity = Gravity.CENTER;

        Button button = findViewById(R.id.button);
        button.setText("huh");
        button.setLayoutParams(linearParams);

        for (int i = 0; i < 20; i++) {
            Button buttons = new Button(this);
            buttons.setText("heh" + (i + 1));
            buttons.setY(button.getY() + 20 *i );
            buttons.setLayoutParams(linearParams);

            linearLayout.addView(buttons);

        }

And the xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="@+id/shop_constraint_layout"
    android:layout_width="match_parent"h=
    android:layout_height="match_parent"
    tools:context=".ui.shop.ShopActivity"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

        <android.support.constraint.Guideline
            android:id="@+id/guideline21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.21" />


        <ScrollView
            android:id="@+id/scv"
            android:layout_width="match_parent"
            android:fillViewport="false"
            android:layout_height="match_parent"
            android:layout_marginTop="140dp"
            android:background="#f0ffa0"
            android:scrollIndicators="right"
            android:scrollbarStyle="insideOverlay"
            android:scrollbars="vertical"
            app:layout_constraintTop_toTopOf="@+id/guideline21"
            tools:layout_editor_absoluteX="16dp">


                <LinearLayout
                    android:id="@+id/LinearLayoutu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.196"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/guideline21">

                        <Button
                            android:id="@+id/button"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Button" />

                </LinearLayout>

        </ScrollView>

</android.support.constraint.ConstraintLayout>

I expect the buttons created to be in the center of the screen, aligned vertically, one above the other, at a distance of 20p, and to be scrollable trough.

dominicoder :
for (int i = 0; i < 20; i++) {
    Button buttons = new Button(this);
    buttons.setText("heh" + (i + 1));
    buttons.setY(button.getY() + 20 *i ); // <- Remove this
    buttons.setLayoutParams(linearParams);
    linearLayout.addView(buttons);
}

LinearlyLayout automatically arranges its children one after the other. There is no need to explictly set the "Y" position on any of the views. If you want spacing between them, you would use padding.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162123&siteId=1
Recommended