0

I'm trying to add programmatically a variable quantity of buttons to an Activity.

So I'm starting from a populated hashmap like this:

Map<String, String> map = new HashMap<String, String>();

Then I loop to get data and to make the buttons:

int k = 0;
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layrelative);
for(Entry<String,String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            Button b = new Button(Peli.this);
            b.setId(k);
            b.setText(key);
            b.setOnClickListener(Peli.this);
            layout.addView(b);
            k++;
        }

The problem is that I'm only getting not more than one button, but if I Log the loop the data is OK to generate more than one button, wich by the way, si always the last data from the last iteration.

What am I doing wrong? I've tested also with an Iterator with the same results, so i've think that my mistake is with the addView method or with the creation of every button view.

Thanks in advance.

Martin Revert
  • 3,242
  • 2
  • 30
  • 33

1 Answers1

2

You arent setting any of the Button's properties in terms of where you want them to be laid out. This is a RelativeLayout. The default is top-left. Esentially you are laying out many buttons, but they are all one on top of another in the Z Direction!!!

Take a look at this How to lay out Views in RelativeLayout programmatically?

Community
  • 1
  • 1
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33