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 ..

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

hi stappo could u help me over here -> stackoverflow.com/questions/76786965/…
hi android dev, today i'm busy but next days, if your question will be open i'll try to help you
but can you help me today im really in trouble ...i can also give my all points to you if you want

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.