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().
Asked
Active
Viewed 2.7k times
36
-
what you exactly want ?? describe more – Shailendra Singh Rajawat Nov 20 '11 at 10:30
6 Answers
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

Dmytro Danylyk
- 19,684
- 11
- 62
- 68
-
6You might also keep in mind that if the view isn't inflated it's width and height will be 0. – Joru Nov 20 '11 at 13:12
-
You are right Joru. I used the ViewTreeObserver and resolved my issue. Thanks to all who tried to answer. – Pavankumar Vijapur Nov 20 '11 at 14:44
-
4
-
view.measure takes View.MeasureSpec as an arguments. [@Sandip Jadhav's answer](https://stackoverflow.com/a/9426895/7599300) is up to date. – Hrishikesh Kadam Sep 26 '18 at 14:07
39
@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.

Sandip Jadhav
- 7,377
- 8
- 44
- 76
-
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 -> http://stackoverflow.com/a/24054428/1815624 – CrandellWS Apr 30 '16 at 10:16
-
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. – qix Aug 10 '18 at 06:18
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();
}
});

nhahtdh
- 55,989
- 15
- 126
- 162

AdrianoCelentano
- 2,461
- 3
- 31
- 42
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();

Jason Yin
- 101
- 1
- 2
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.

Community
- 1
- 1

Richard Le Mesurier
- 29,432
- 22
- 140
- 255
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();
}
}

Zethus
- 1