3

I want to get the height/width of the Middle(Body) layout in onCreate() method.

My main.xml is :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_bg11" >

<RelativeLayout
    android:id="@+id/rl_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" >

    <include layout="@layout/title" />
</RelativeLayout>

<ScrollView
    android:id="@+id/svtest"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/rl_music_controller"
    android:layout_below="@+id/rl_title" >

    <RelativeLayout
        android:id="@+id/rl12"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TableLayout
            android:id="@+id/tbl_vandana"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:gravity="center_horizontal"
            android:paddingTop="30dp" >
        </TableLayout>
    </RelativeLayout>
</ScrollView>

<RelativeLayout
    android:id="@+id/rl_music_controller"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <include layout="@layout/controller" />
</RelativeLayout>

I am adding Textview dynamically in TableLayout. id :

  1. rl_title is my title layout

  2. svtest is my Middle(Body) Content.

  3. rl_music_controller is my footer layout.

I am referring this link. But i don't understand exactly what i do?

Community
  • 1
  • 1
Dhruv
  • 1,862
  • 3
  • 20
  • 38
  • Have You tried this http://stackoverflow.com/questions/12068945/get-layout-height-and-width-at-run-time-android ? – sandrstar Jul 09 '13 at 07:07
  • I have device Height/width by using "Display" Class. – Dhruv Jul 09 '13 at 07:10
  • As per Your question You need size of svtest layout, right? – sandrstar Jul 09 '13 at 07:16
  • yup.. My concept to calculate body portion is that : calculate both title and footer layout height. And i have Whole device height. Deduct them from whole device height. Is it right way? – Dhruv Jul 09 '13 at 07:20
  • Yes, it's possible way, but using provided link You can probably get size of svtest layout directly. – sandrstar Jul 09 '13 at 07:23
  • but i can't understand how can i do that? Can you please elaborate how can i apply this concept? – Dhruv Jul 09 '13 at 07:28
  • Have You checked http://stackoverflow.com/questions/12068945/get-layout-height-and-width-at-run-time-android answers? – sandrstar Jul 09 '13 at 07:31

3 Answers3

11

Added: This applies for all types of layout. Not only ScrollView.

getWidth() and getHeight() methods returns 0 when layouts width and height are set as match_parent and wrap_content. dimensions are not measured.

The right methods to use to get measured dimensions are getMeasuredWidth() and getMeasuredHeight().

Note: measured dimensions in onCreate method are not initialized yet.

The correct way and place is (snippet can be added anywhere including onCreate):

ScrollView scrollView = (ScrollView)findViewById(R.id.svtest);
ViewTreeObserver vto = scrollView.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        if (Build.VERSION.SDK_INT < 16)
            scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
        else
            scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(listener);

        int width  = scrollView.getMeasuredWidth();
        int height = scrollView.getMeasuredHeight(); 

        // postpone any calculation depend on it to here.
        // regardless what it is. UI or http connection.
    } 
});

Some answers tried to do it onStart() method. but, they tried to call getWidth() and getHeight() methods. Try getMeasuredWidth() and getMeasuredHeight(). it works without adding OnGlobalLayoutListener. The listener is only required if you want to get the dimensions in the on create method. in any later stage the listener is not required because by then the dimensions will be measured(on start for example).

hasan
  • 23,815
  • 10
  • 63
  • 101
  • The snippet works well but how can I do to get `width`and `height` in the function which calls `addOnGlobalLayoutListener` so I can assign the values to a `Point` declared in this function? – statox May 01 '15 at 16:16
  • int width = scrollView.getMeasuredWidth(); int height = scrollView.getMeasuredHeight(); can be called any where after the onCreate method so you can add them to your function. that was the point of OnGlobalLayoutListener – hasan May 01 '15 at 19:24
  • I call `getMeasuredWidth` from `onStart` and I still get `0`. The `OnGlobalLayoutListener` is necessary. – GSerg Apr 10 '16 at 12:38
1

Firstly, don't get view dimensions in onCreate() since the view might not have been initialized yet. You can do it in the onStart() method. All the views will have been inflated and laid out on the screen at that point:

@Override
    protected void onStart() {
        super.onStart();
        ScrollView scrollView = (ScrollView) findViewById(R.id.svtest);
        int width = scrollView.getWidth();
        int height = scrollView.getHeight();
    }
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Are you calling it in onStart()? Does the scrollview have inflated elements inside it? If it has nothing inside it, it will return 0. – Anup Cowkur Jul 09 '13 at 07:23
  • this is the point those don't work when width and height set like wrap_content and match_parent – hasan Oct 24 '13 at 09:04
  • This is not working. In onStart I just get 0 for whatever dimensions. – Sotti Jun 18 '14 at 13:07
0

I find overriding the following method prevents the width and height being 0. At this stage of the Activity all of the View's in the Activity should be inflated.

public void onWindowFocusChanged (boolean hasFocus)
{
     super.onWindowFocusChanged(hasFocus);
     ScrollView scrollView = (ScrollView) findViewById(R.id.svtest);
     int width = scrollView.getWidth();
     int height = scrollView.getHeight();
}
Phil Applegate
  • 567
  • 2
  • 9