0

I have a button with dimensions:

btn1.width = 100;
btn2.height = 150;

I used the following code to resize:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) btn1.getLayoutParams();
layoutParams.width = 200;
layoutParams.height = 300;
btn1.setLayoutParams(layoutParams);

When I did this:

int newSize = btn1.getWidth;

The newSize has value 100, but not 200. It resized the button well, but after the resize, btn1.getWidth still holds the previous value. How can I instantly get the set size?

IronMan007
  • 105
  • 4
  • 14
user3240604
  • 407
  • 1
  • 8
  • 22

2 Answers2

0

The actual width of the View does not get updated immediately.

Your best bet is to cache the size when you set it.

manabreak
  • 5,415
  • 7
  • 39
  • 96
0

You will not directly get the height/width after setting to a view. If you want then you can get in "post" method:

btn1.setWidth(200);
btn1.setHeight(300)
btn1.post(new Runnable() {
         @Override
         public void run() {
               Log.i("Tag", "Width:"+ btn1.getWidth() + " Height:" + btn1.getHeight());
         }
});
Brijesh Chopda
  • 195
  • 2
  • 11