2

I have a LinearLayout, and this LinearLayout will hold dynamically placed views. I need to find out what the width of the children of LinearLayout, however this has to be done in onCreate method. From researching I've found out that you can't use getWidth from this method. So instead I'm using onWindowFocusChanged, which works for the parent LinearLayout (returning me the actual size), but it doesn't work with its children.

Another thing I noticed is that when the screen is fading away and the screen is locked, I can see at the logs the actual width of the children being returned (I think the activity is being paused).

I'm really stuck and this is needed because I need to dynamically place those views depending on the children width.

1

3 Answers 3

5

You might be able to get with the below. But as others pointed out, this probably isn't a great idea.

LinearLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
LinearLayout.getMeasuredWidth();
7
  • because if you call it before other views are still in the process of the layouting , you will get a wrong value .of course , it depends on your implementation . better get the size at the correct time and not in the onCreate() method. also , i think this code make the layout process occur twice for this view ,so it affects performance too (depending on the layout, of course) . Jun 7, 2012 at 13:27
  • It could be useful if the size of your linearLayout is not dependent on the size of any other views which have not yet been added. though, anything you do based on this size is most likely a bad practice with a bad solution.
    – ab11
    Jun 7, 2012 at 13:31
  • @androiddeveloper @ab11 I need to say I'm doing this on onWindowFocusChanged, so the parent LinearLayout is already drawn. Jun 7, 2012 at 13:33
  • well so you don't need to call "measure" , since it's already measured, no? Jun 7, 2012 at 13:57
  • @androiddeveloper I tried without it, didn't work. By the way sorry for the super late answer. Jul 3, 2012 at 12:34
3

inside the onCreate , views still can't know the state of the nearby views and the children ,etc... so only after all is prepared and the layout process is done , you can get the size of views .

here's a quick code for getting the size of the view just before it's being drawn:

private static void runJustBeforeBeingDrawn(final View view, final Runnable runnable)
{
    final ViewTreeObserver vto = view.getViewTreeObserver();
    final OnPreDrawListener preDrawListener = new OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw()
        {
            Log.d(App.APPLICATION_TAG, CLASS_TAG + "onpredraw");
            runnable.run();
            final ViewTreeObserver vto = view.getViewTreeObserver();
            vto.removeOnPreDrawListener(this);
            return true;
        }
    };
    vto.addOnPreDrawListener(preDrawListener);
}

alternatively , you can use addOnGlobalLayoutListener instead of addOnPreDrawListener if you wish.

example of usage :

runJustBeforeBeingDrawn(view,new Runnable()
{
  @Override
  public void run()
  {
    int width=view.getWidth();
    int height=view.getHeight();
  }
});

another approach is to use onWindowFocusChanged (and check that hasFocus==true) , but that's not always the best way ( only use for simple views-creation, not for dynamic creations)

EDIT: Alternative to runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126

4
  • What exactly would be that runnable? Jun 7, 2012 at 13:17
  • you need to implement it (you can put there "new Runnable()" and it will automatically show what you need to implement) . inside its run() method , you can call getWidth() or getHeight() on the view in order to get its correct width and height instead of 0 . anyway , updated my answer to show an example of how to use it . Jun 7, 2012 at 13:23
  • Thanks. Is this a more viable solution than the getMeasuredWidth() one? Jun 7, 2012 at 13:38
  • yep , though i do agree that it looks weird , and it's even weirder that i didn't find any tutorial about this , since it's a very common problem . an alternative would be to use "addOnGlobalLayoutListener" instead of this method . Jun 7, 2012 at 13:56
0

https://stackoverflow.com/a/3594216/1397218

So you should somehow change your logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.