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.

Comic Sans MS Lover
  • 1,729
  • 5
  • 26
  • 52
  • possible duplicate of [Android get width returns 0](http://stackoverflow.com/questions/3591784/android-get-width-returns-0) – kabuko Jun 06 '12 at 22:55

3 Answers3

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();
ab11
  • 19,770
  • 42
  • 120
  • 207
  • 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) . – android developer Jun 07 '12 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 07 '12 at 13:31
  • @androiddeveloper @ab11 I need to say I'm doing this on `onWindowFocusChanged`, so the parent LinearLayout is already drawn. – Comic Sans MS Lover Jun 07 '12 at 13:33
  • well so you don't need to call "measure" , since it's already measured, no? – android developer Jun 07 '12 at 13:57
  • @androiddeveloper I tried without it, didn't work. By the way sorry for the super late answer. – Comic Sans MS Lover Jul 03 '12 at 12:34
  • anyway , try out my suggestion , and keep the size in a variable for later use. – android developer Jul 03 '12 at 19:23
  • @Gabriel i think it's not a good thing to use, as it won't work for dynamically created views, and it doesn't consider the layout rules of the views. try it out on a view with layout_width of match_parent, for example. – android developer Dec 27 '13 at 14:28
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

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • What exactly would be that runnable? – Comic Sans MS Lover Jun 07 '12 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 . – android developer Jun 07 '12 at 13:23
  • Thanks. Is this a more viable solution than the `getMeasuredWidth()` one? – Comic Sans MS Lover Jun 07 '12 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 . – android developer Jun 07 '12 at 13:56
0

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

So you should somehow change your logic.

Community
  • 1
  • 1
efpies
  • 3,625
  • 6
  • 34
  • 45