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
  • 112
  • 8

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
  • 89
  • 4
  • hi stappo could u help me over here -> https://stackoverflow.com/questions/76786965/facing-issue-while-validating-amongst-the-edittexts-in-recyclerview – android dev Jul 28 '23 at 12:31
  • hi android dev, today i'm busy but next days, if your question will be open i'll try to help you – Stappo Jul 28 '23 at 12:52
  • but can you help me today im really in trouble ...i can also give my all points to you if you want – android dev Jul 28 '23 at 12:55