Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic number of elements layout problem

I'm trying to create an Activity with dynamic number of Buttons, each of them representing a Shadow object.

I'm not sure if it's possible to do with the XML file, so I decided to use Java code instead.

The whole idea of displaying all of the List elements is working - I have a button generated for every Shadow in the list.

My problem is that the buttons are all in the same place, the largest one covers the others. How can I make them display properly, e.g. with 16dp vertical space between them?

Here's the class code:

package com.jackw.nfshadow;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.biernacki.nfshadow.tagdiscoveredactivities.Shadow;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ShadowLibrary extends AppCompatActivity {

    private List<Shadow> savedShadows;
    private ConstraintLayout layout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(savedInstanceState != null) {
            super.onCreate(savedInstanceState);
        }else{
            super.onCreate(new Bundle());
        }
        setContentView(R.layout.activity_shadow_library);
        layout  = findViewById(R.id.shadow_library);
        setButtonNames();
    }

    private void setButtonNames() {
        if(Main.savedShadows != null && Main.savedShadows.size() > 0){
            savedShadows = Main.savedShadows.stream()
                    .sorted(Comparator.comparing(Shadow::getName))
                    .collect(Collectors.toList());

            savedShadows.forEach(shadow -> {
                Button button = new Button(this);
                button.setId(View.generateViewId());
                button.setText(shadow.getName());
                layout.addView(button);
            });

        }
    }
}

Since I'm totally new to Android, I'd also appreciate any information about other ways of creating dynamic content/small review of the code I've written above.

UPDATE: I've edited the setButtonNames() a bit, still trying to use ConstraintLayout and connect() method, but for some reason it still doesn't work. I can't find what's wrong with this one.

savedShadows.forEach(shadow -> {
                Button button = new Button(this);
                button.setId(View.generateViewId());
                button.setText(shadow.getName());

                if(shadow == savedShadows.get(0)){
                    previousButtonId = button.getId();
                }else{
                    constraints.connect(button.getId(), ConstraintSet.TOP, previousButtonId, ConstraintSet.BOTTOM, 16);
                    previousButtonId = button.getId();
                }
                constraints.applyTo(layout);
                layout.addView(button);
            });
        }
}
like image 335
Jack_Russell Avatar asked Dec 10 '25 11:12

Jack_Russell


1 Answers

Your problem is you are not adding constraints for your Button. You can do it like in this example, where layout is your ConstraintLayout:

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout);
constraintSet.connect(someButton.getId(), ConstraintSet.TOP, 
                       anotherButton.getId(), ConstraintSet.BOTTOM);
constraintSet.applyTo(constraintLayout);

You need to add constraints as you want. Or replace your ConstraintLayout with LinearLayout and it will automatically add buttons correctly without no need to work with constraints.

like image 97
Nazarii Moshenskiy Avatar answered Dec 13 '25 00:12

Nazarii Moshenskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!