Calculate view measure before display it

Go To StackoverFlow.com

3

I am trying to add views to LinearLayout dynamically as much as it possible (depending on screen width).

I do this before the LinearLayout displays on screen.

My LinearLayout:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" 
    android:background="#666"/>

My view to display in LinearLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp"
    android:background="#999">
    <ImageView
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/no_photo"/>
</FrameLayout>

I add views to layout:

int allItems = 50;
int currentItem = 0;
while(currentItem < allItems)
{
    FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fl, null);

    linearLayout.addView(view);

    if (linearLayout.getMeasuredWidth() >= this.getWidth())
    {
        linearLayout.removeView(view);
        break;
    }
}

but linearLayout.getMeasuredWidth() and this.getWidth() is 0;

I know, that i must use View.measure method to calculate view size before it became visible, but i don't know where and how it use.

2012-04-04 07:16
by Nik
Possible dupe of: http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r/4406090#440609 - Jason LeBrun 2012-04-04 07:19
check this http://stackoverflow.com/questions/9498762/dynamically-adding-views-to-horizontal-linearlayout-goes-out-of-the-scree - Nishant 2012-04-04 07:21
"android:background="#666"" - I see you are using the magic number just like I d - Jesus Christ 2017-02-13 16:11


8

Edit your code as below :

Display display = getWindowManager().getDefaultDisplay();
int maxWidth = display.getWidth();
int widthSoFar=0;

int allItems = 50;
int currentItem = 0;

while(currentItem < allItems) {
  FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fl, null);

  linearLayout.addView(view);

  view .measure(0, 0);
  widthSoFar = widthSoFar + view.getMeasuredWidth();


  if (widthSoFar >= maxWidth) {
    linearLayout.removeView(view);
    break;
  }
}

Hope this helps you

2012-04-04 07:27
by Nishant
But if my parent view not take full screen width - Nik 2012-04-04 07:31
Then you can put your parent view width inside maxWidth by calling parentView.getMeasureWidth(). which is your parent view here - Nishant 2012-04-04 08:40
Does the code helps you - Nishant 2012-04-04 10:48
I work on other part of my program so far - Nik 2012-04-04 11:27
great the code works for yo - Nishant 2012-04-05 07:06
1) get screen width; 2) add my view to layout 3) measure layout width 4) check if layout measured width more than screen width 5) if true remove last added vie - Nik 2012-04-05 07:08
what you want to tell me - Nishant 2012-04-05 07:09
what is measure(0,0) ? what are theese magic constants - Korniltsev Anatoly 2012-10-04 10:08
@KorniltsevAnatoly Check it out here http://stackoverflow.com/questions/3937238/android-view-measureint-int-always-wants-to-be-0- - Nishant 2012-10-04 12:22
you are measuring each view that is being added. And you ignore margins, weights, layout params btw. Isn't there a generic way to just measure the linearlayout - Radu Simionescu 2014-10-30 10:52
Ads