Creating an array of buttons programatically in android -


i learning android , trying create array of buttons app in following manner:

 linearlayout answer_layout = (linearlayout)findviewbyid(r.id.answer_layout);     idcount = answer_layout.getid() + 1000;     for(int = 0 ; i<letters.length ; i++)     {         button b = new button(this);         b.settext(letters[i]);         b.settypeface(null, typeface.bold);         b.setbackgroundresource(r.drawable.puzzletilebg);         b.settextsize(typedvalue.complex_unit_sp, 15);         b.setincludefontpadding(false);         b.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content));         b.setid(idcount + i);         b.settag(integer.valueof(i));         bletters[i] = b;         answer_layout.addview(b);      } } 

when run code, able row of buttons depending on length value of string. problem buttons appear stretched , when length value more 7, buttons dont appear. problem tried implementing method suggested here(how programmatically add buttons layout 1 one in several lines?) didnt result. parameters have use make shape of buttons perfect square , make sure of same size screen sizes? button background drawable size 50x50.

if want perfect square change

linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content)); 

to

linearlayout.layoutparams(50, 50); 

and ofc if linearlayour horizontal buttons dissapear on edge of screen. can use gridlayout instead of linearlayout, if want linearlayout recommend: flowlayout

example:

enter image description here last edit: sorry not autofitlayout flowlayout edited answer;

solve problem?


Comments