36

How to find the Width of the a view before the view is displayed? I do not want to add a custom view and tap the dimensions in the OnChanged().

1
  • what you exactly want ?? describe more Commented Nov 20, 2011 at 10:30

6 Answers 6

50
Display display = getWindowManager().getDefaultDisplay(); View view = findViewById(R.id.YOUR_VIEW_ID); view.measure(display.getWidth(), display.getHeight()); view.getMeasuredWidth(); // view width view.getMeasuredHeight(); //view height 
Sign up to request clarification or add additional context in comments.

4 Comments

You might also keep in mind that if the view isn't inflated it's width and height will be 0.
You are right Joru. I used the ViewTreeObserver and resolved my issue. Thanks to all who tried to answer.
view.measure(display.getWidth(), display.getHeight()) is deprecated
view.measure takes View.MeasureSpec as an arguments. @Sandip Jadhav's answer is up to date.
40

@dmytrodanylyk -- I think it will return width & height as 0 so you need to use following thing

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View contentview = inflater.inflate(R.layout.YOURLAYOUTNAME, null, false); contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int width = contentview.getMeasuredWidth(); int height = contentview.getMeasuredHeight(); 

It will give you right height & width.

2 Comments

That is what I need to determine if I should start a new row of checkboxes, first making it a single line and then prevent overflow by calculating the width...perfect thank you. coming from -> stackoverflow.com/a/24054428/1815624
Yes, this is a handy way to figure out the intrinsic size of a view based on its initial xml layout, and useful if say you're doing some dynamic calculations later on for layouts.
13

You should use OnGlobalLayoutListener which is called for changes on the view, but before it is drawn.

costumView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { costumView.getWidth(); } }); 

Comments

10
Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); view.measure(size.x, size.y); int width = view.getMeasuredWidth(); int height = view.getMeasuredHeight(); 

Comments

4

I like to use this technique as per my answer on a related question:

Post a runnable from onCreate() that will be executed when the view has been created:

 contentView = findViewById(android.R.id.content); contentView.post(new Runnable() { public void run() { contentHeight = contentView.getHeight(); } }); 

This code will run on the main UI thread, after onCreate() has finished.

Comments

0

This is the best way...... WORK!!!

public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (fondo_iconos_height==0) { // to ensure that this does not happen more than once fondo_iconos.getLocationOnScreen(loc); fondo_iconos_height = fondo_iconos.getHeight(); redraw_function(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.