Reputation: 60
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!
Upvotes: 1
Views: 4939
Reputation: 83311
Using an AbsoluteLayout
, you need to use AbsoluteLayout.LayoutParams
, rather than ViewGroup.LayoutParams
.
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(width, height, 0, 0);
button.setLayoutParams(params);
Also note that the proper way to set the width and height of a View
is to do so via the LayoutParams
. See ViewGroup.LayoutParams
and View.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
(or LinearLayout
, etc.) instead... AbsoluteLayout
is deprecated, and for very good reason. There are so many different Android devices with different sized screens now, AbsoluteLayout
just won't work across them all. Never, ever, ever using AbsoluteLayout
is always good practice :).
Upvotes: 1
Reputation: 866
i think it will be
LayoutParams params = new LayoutParams(width,height);
button.setLayoutParams(params);
Upvotes: 3