4

I'm trying to learn the basics of android and when I call RelativeLayout.getWidth I get 0 as a value instead of the expected size of the layout.

I have a RelativeLayout filling my screen:

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/activity_one>
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:id="@+id/test"/>
    </RelativeLayout >

But when I use the code below in the oncreate(), I get 0 instead of the expected screen width 720.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        RelativeLayout rl= (RelativeLayout) findViewById(R.id.activity_one);

        TextView tempTextview = (TextView) findViewById(R.id.test);

        tempTextview.setText("W: " + rl.getWidth() + " - H:" + rl.getHeight());
}

I must be doing a very stupid mistake but I can't see what.

Slamit
  • 465
  • 1
  • 6
  • 21

4 Answers4

4

You should get width here when Your UI has been loaded and then only width can be invoked of layout.:

@Override
 public void onWindowFocusChanged(boolean hasFocus) {

  super.onWindowFocusChanged(hasFocus);
//paste your code to get your layout params.
 }
Androider
  • 3,833
  • 2
  • 14
  • 24
  • Indeed as stated there: http://stackoverflow.com/questions/3591784/getwidth-and-getheight-of-view-returns-0 Thanks :-) – Slamit Sep 05 '15 at 16:55
  • i don't know wherever it is available, but i know it's available on http://developer.android.com/, but we are here to help each other, nothing more. – Androider Sep 05 '15 at 17:03
1

The width is not set until onMeasure is called (Which is when the dimensions are actually measured and this method in turn calls setMeasuredDimension).

You could get a ViewTreeObserver object for the particular layout and implement the ViewTreeObserver.OnDrawListener and pass this implementation to the observer objects addOnDrawListener function,

Get the height and width inside this observer implementation and make changes to whatever other view item you want based of these new values that way everything stays consistent even if the layout height and width were to change in the future dude to maybe orientation changes?

There are plenty of other events in the viewTreeObserver objects just find one that suits your needs and write listeners to them. Here is a link to the documentation for ViewTreeObserver http://developer.android.com/reference/android/view/ViewTreeObserver.html

Bhargav
  • 8,118
  • 6
  • 40
  • 63
0

You're calling the getWidth() / getHeight() too early. The UI isn't fully laid out and sized yet.

LonelyIdiot
  • 789
  • 2
  • 6
  • 16
0

If i remember correctly, the variables like width & height of Layout elements is set AFTER the onCreate() method. Had some similar issues and fixed them by getting height/width in a different place in the logical flow of your app

TimoL
  • 1
  • 2