0

I'm trying to make a 100x100 px button and add it to a gridlayout on the position 3,3

Button btn = new Button(this);
btn.setLayoutParams(new ViewGroup.LayoutParams(100,100));
GridLayout grid4x4 = (GridLayout)findViewById(R.id.grid4x4);

//I've narrowed it down to the conclusion that this line is the problem:
grid4x4.addView(btn,new GridLayout.LayoutParams(GridLayout.spec(3),GridLayout.spec(3)));
//if i just use:
//grid4x4.addView(btn); I get the button the size I want it, but I need it to be on 
//the 3,3 position of the grid.

This "almost" works, excepting the fact that the button added is bigger than 100x100px as you can see I'm trying to fix the size I want yet for some reason that's not the size I get.

Artemio Ramirez
  • 1,116
  • 1
  • 10
  • 23

2 Answers2

0

in your res/values/dimens.xml add <dimen name="mybuttonsize">100px</dimen>

int size = (int) getResources().getDimension(R.dimen.mybuttonsize);

change your code to:

btn.setMaxWidth(size);
btn.setMaxHeight(size);
btn.setLayoutParams(new ViewGroup.LayoutParams(size,size));
Rami
  • 7,879
  • 12
  • 36
  • 66
  • exact same result, according to the documentation the public LayoutParams constructor already takes the int as px so even when I expected it would be the same I tried and it did gave me the same results. – Artemio Ramirez Nov 12 '14 at 20:41
  • if i use 100dp in the xml the button does come bigger, but if i set it to 50dp wich would be the equivalent for 100px (on the screen im testing) it still doesnt work. I do know it can be a 100x100 px button because I already have another one not added programatically tht correctly shows the way I want it to be. – Artemio Ramirez Nov 12 '14 at 20:48
0

This might not be the ideal solution, but this worked exactly the way I wanted:

Button btn = new Button(this);
btn.setLayoutParams(new ViewGroup.LayoutParams(100,100));
GridLayout grid4x4 = (GridLayout)findViewById(R.id.grid4x4);
grid4x4.addView(btn);
((GridLayout.LayoutParams) btn.getLayoutParams()).rowSpec = GridLayout.spec(3);
((GridLayout.LayoutParams) btn.getLayoutParams()).columnSpec = GridLayout.spec(3);

Oim~

Artemio Ramirez
  • 1,116
  • 1
  • 10
  • 23