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.

2
  • @Colin Indeed thanks! - should I delet this post? Commented Sep 5, 2015 at 16:53
  • answers and questions can always be built upon Commented Sep 5, 2015 at 17:17

4 Answers 4

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. } 
Sign up to request clarification or add additional context in comments.

2 Comments

Indeed as stated there: stackoverflow.com/questions/3591784/… Thanks :-)
i don't know wherever it is available, but i know it's available on developer.android.com, but we are here to help each other, nothing more.
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

1 Comment

Super good info, thanks for that and for the link! More to learn :) !
0

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.