简体   繁体   中英

Dynamically List Buttons in Android Studio (Relative Layout)

So I have a relative layout on my activity and I'm trying to have my buttons be dynamically instantiated based on data in a text file. The data is being read correctly and each button is being instantiated but only the first one instantiates correctly, all other buttons instantiate above the first button in the same position, overlapping each other. Code I am using:

RelativeLayout layout = (RelativeLayout)findViewById(R.id.relLayout);
            Resources r = getResources();
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, r.getDisplayMetrics());
            int prevId = 0;
            for(int i = 0; i < data.size(); i++) {
                Button btn = new Button(this);
                btn.setText(data.get(i).substring(0, data.get(i).indexOf(',')));
                btn.setWidth((int) px);
                btn.setId(i);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                if(i > 0) {
                    params.addRule(RelativeLayout.BELOW, prevId);
                } else {
                    params.addRule(RelativeLayout.BELOW, findViewById(R.id.addClass).getId());
                }
                layout.addView(btn, params);
                prevId = btn.getId();
            }

The only difference I can see between the first and other buttons is that the first button sets its position relative to a static anchor I have while the other buttons alignments are anchored by the previous button. (Or well, supposed to be). It should end of in the following format:

Classes
Add Class (static button)
dynamicbutton1
dynamicbutton2
. . .

I'm not sure if this will solve your problem, but you could try it. Remove prevID and place it under the current value of i-1. Then just test for whether i==0. To me that's a less clumsy way of doing it.

And add the button to the view within the if else, to ensure the changes to param have taken effect.

//int prevId = 0;
    for(int i = 0; i < data.size(); i++) {
        Button btn = new Button(this);
        btn.setText(data.get(i).substring(0, data.get(i).indexOf(',')));
        btn.setWidth((int) px);
        btn.setId(i+1);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            if(i = 0) {
                params.addRule(RelativeLayout.BELOW, findViewById(R.id.addClass).getId());
                layout.addView(btn, params);

            } else {
                params.addRule(RelativeLayout.BELOW, i);
                layout.addView(btn, params);
            }

        }

After reading up about this How can I assign an ID to a view programmatically? . It seems, that the id has to be greater than zero. So I've altered the code to be i+1, then the previous id will be i.

There is another issue that these ids may not be unique, as there may be other ids parsed from your xml with ids within the ranges of your buttons. This will mean that the buttons are being positioned relative to another element entirely.

This answer goes into depth about how to avoid this duplication. https://stackoverflow.com/a/13241629/3956566

Depending on how you want the buttons arranged, using a different Layout instead of the RelativeLayout would make things easier. Eg use a vertical LinearLayout to arrange the buttons vertically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM