0

How i can get Width or Height of a View in Android ? when using getWidth() getHeight() methods they are returning 0

and another sub question is getWidth() return value in dp or px ..

FsCode
  • 90
  • 7

1 Answers1

1

Your view has not rendered yet. You have to wait it!

Use post method.

yourView.post(new Runnable() {
        @Override
        public void run() {
            int height = yourView.getHeight();
            int width = yourView.getWidth();
        }
    });

height and width value will be in pixel format

If you get this exception:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

put your .post() code in a runOnUiThread method.

Stappo
  • 49
  • 4