0

I don't understand how to do this. Can someone explain to me how to do that with an example using an ImageView or a simple button?

tshepang
  • 12,111
  • 21
  • 91
  • 136
manuel.koliqi
  • 313
  • 1
  • 3
  • 11

2 Answers2

1

I do something like this in onCreate to get the correct width and height:

    final ViewTreeObserver vto = view.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            if (Build.VERSION.SDK_INT < 16) {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }

            width = view.getWidth();
            height = view.getHeight();

            // using the method Alécio mention
            setupOtherViewsThatDependOnWidthAndHeight();

        }
    });

The case here is that size and height does not return correct values before screen is done rendering.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
0

You can use the Layout parameters for this. You can use it like this, passing the width and height programmatically (100,100 respectively)

LayoutParams params = new LayoutParams(100,100);
view.setLayoutParams(params);
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74