applepie applepie - 1 year ago 77
Android Question

Why is my button height not changing?

I have some buttons in a pop up window. I can adjust the button's width just fine but I do not know why I cannot adjust its height.
Here is part of my code:

public void popup(RelativeLayout v){
RelativeLayout r = new RelativeLayout(this);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// w is the width of the device screen, tested it isn't 0
buttonParams.setMargins(w/20, w / 10, 0, 0);
Button ok = new Button(this);
ok.setWidth(w / 6);
ok.setHeight(w / 35);
ok.setTextSize(h / 50);
p.showAtLocation(v, 0, w / 4, w / 4);

I understand that I can change the height by changing
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)

to specific numbers, but I do not know why I am able to adjust the width but not height with my code.

Answer Source

LayoutParams is created to handle size of child view inside its parent. So i think you should

remove ok.setWidth and ok.setHeight

change layoutparams contructor to

RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams( w / 6, w / 35);

I tested and it worked. Please try !

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download