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 :
rl_title is my title layout
svtest is my Middle(Body) Content.
rl_music_controller is my footer layout.
I am referring this link. But i don't understand exactly what i do?
Firstly, don't get view dimensions in
onCreate()
since the view might not have been initialized yet. You can do it in theonStart()
method. All the views will have been inflated and laid out on the screen at that point:getWidth()
andgetHeight()
methods returns 0 when layouts width and height are set asmatch_parent
andwrap_content
. dimensions are not measured.The right methods to use to get measured dimensions are
getMeasuredWidth()
andgetMeasuredHeight()
.The correct way and place is (snippet can be added anywhere including
onCreate
):Some answers tried to do it onStart() method. but, they tried to call
getWidth()
andgetHeight()
methods. TrygetMeasuredWidth()
andgetMeasuredHeight()
. it works without addingOnGlobalLayoutListener
. 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).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.