-1

I need to get the height and width of the View before adding into layout or its is display. I have tried with using MeasuredHeight and MeasuredWidth of the view but its retursn as 0. Could you please help me out to resolve this?

Below code snippet I have tried out:

 int height = Column.MeasuredHeight;
 int width  = Column.MeasuredWidth;
Parthiban
  • 168
  • 1
  • 13
  • Why you want it before setting layout? – R.R.M May 03 '17 at 07:28
  • try this http://stackoverflow.com/a/9426895/5577679 – tompadre May 03 '17 at 07:57
  • @tompadre Thanks for the update. Column.Measure(View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified), View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified)); int colHeight = Column.MeasuredHeight; By using this solution, i am not getting actual height of the column. – Parthiban May 03 '17 at 08:54
  • then try the answer above. http://stackoverflow.com/a/16437972/5577679 Proper way to get dimensions before inflating view is by ViewTreeObserver which is explained in the answer above. Official documentation is here https://developer.android.com/reference/android/view/ViewTreeObserver.html – tompadre May 03 '17 at 09:19

3 Answers3

1

You need to use a Layout change event to not receive 0 as the height. If you ask it too soon, the layout isn't drawn yet. Use the ViewTreeObserver of the layout that you want to measure.

LinearLayout linearLayout = FindViewById<LinearLayout>(Resource.Id.linearLayout);
ViewTreeObserver vto = linearLayout.ViewTreeObserver;

vto.GlobalLayout += (sender, args) => {
    System.Console.WriteLine (linearLayout.Height);
}; 
Dennis Reep
  • 97
  • 2
  • 9
0

Try view.getWidth() and view.getHeight()

deepak p
  • 76
  • 2
0
View view= LayoutInflater.from(this).inflate(R.layout.activity_main, null, false);
setContentView(view);
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
Mohammad nabil
  • 1,010
  • 2
  • 12
  • 23