I’m developing a little Android app with some square buttons created dynamically from Java code. I’m using the Absolute.LayoutParams method to give width and height to them, but I get deprecation warnings.
LayoutParams params = new LayoutParams(width,height, 0, 0);
button.setLayoutParams(params);
I tried this too:
button.setHeight(height);
button.setWidth(width);
But it is not working. There is a third way to do it easily and clean?
Thank you in advance!
Using an
AbsoluteLayout, you need to useAbsoluteLayout.LayoutParams, rather thanViewGroup.LayoutParams.Also note that the proper way to set the width and height of a
Viewis to do so via theLayoutParams. SeeViewGroup.LayoutParamsandView.getLayoutParams(). You shouldn’t have to set the width and height manually as you do in your second example.I strongly suggest, however, that you implement this with a
RelativeLayout(orLinearLayout, etc.) instead…AbsoluteLayoutis deprecated, and for very good reason. There are so many different Android devices with different sized screens now,AbsoluteLayoutjust won’t work across them all. Never, ever, ever usingAbsoluteLayoutis always good practice :).