1

Hi I am using this code

View v= inflater.inflate(R.layout.mylayout, null, false);
int width=v.getWidth();
int height=v.getHeight();

but the height and width returns zero, Why is it not working , how to get the height and widht of my view. Thanks in advance

2 Answers2

1

Width and Height of the view can be measured after drawing to the screen. Your inflatted view doesn't yet drawn on the screen so if you want get height and width try this.

View contentview = inflater.inflate(R.layout.mylayout, null, false);
contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int width = contentview.getMeasuredWidth();
int height = contentview.getMeasuredHeight();
Pragnani
  • 20,075
  • 6
  • 49
  • 74
1

getHeight() and getWidth() return 0 when component are not drawn. So all you need to draw the component and get the measurements of the drawn component.