2

I have created linearlayout with layout:height="fill_parent" I have set 3 buttons and one TextView. I am able to see all. But when I want to get get the height of linearlayout. I used the following code in onCreate method. I am getting linear layout height as zero. But My linearlayout height is fill parent. and also i am able to see elements that i have added. Why I am not getting the actual height of my linearlayout.

LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_linearlayout);
System.out.println("...Height..."+mainLayout.getMeasuredHeight());

Thanks Deepak

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • you can try this I have already answered it here http://stackoverflow.com/questions/11964499/how-to-get-height-of-linearlayout/15803364#15803364 – Mohsin Raza Apr 04 '13 at 05:47

3 Answers3

2

Refer to this question

Can't get height of Widgets

Community
  • 1
  • 1
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • Thanks Saurabh, I got height of layout using the following code @Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); System.out.println("...111Height..."+mainLayout.getMeasuredHeight()); } – Sunil Kumar Sahoo May 31 '11 at 12:20
0

Till onCreate, OnResume, onStart view is not yet created. So if you want to get height or width of view do following and get it

       observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                SearchResultActivity.this.sideIndexHeight = SearchResultActivity.this.sideIndex.getHeight();
                ViewTreeObserver obs = sideIndex.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
            }
        });

Please get new TreeObserver to remove, otherwise you will get an exception, TreeObserver is not alive

overriding onWindowFocusChanged method, you get the height or width of view. But if your activity is started as child activity then onWindowFocusChanged method will not get called. So in that case use the above one.

Pritesh Shah
  • 857
  • 1
  • 7
  • 8
0

When an Activity receives focus, it will be requested to draw its layout. The Android framework will handle the procedure for drawing, but the Activity must provide the root node of its layout hierarchy. See this link view drawing and layout padding and margis

vnshetty
  • 20,051
  • 23
  • 64
  • 102