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.

Nik
  • 7,114
  • 8
  • 51
  • 75
  • 2
    Possible dupe of: http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r/4406090#4406090 – Jason LeBrun Apr 04 '12 at 07:19
  • 2
    check this http://stackoverflow.com/questions/9498762/dynamically-adding-views-to-horizontal-linearlayout-goes-out-of-the-screen – Nishant Apr 04 '12 at 07:21
  • "android:background="#666"" - I see you are using the magic number just like I do – Jesus Christ Feb 13 '17 at 16:11

1 Answers1

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

Community
  • 1
  • 1
Nishant
  • 32,082
  • 5
  • 39
  • 53
  • 1
    But if my parent view not take full screen width? – Nik Apr 04 '12 at 07:31
  • Then you can put your parent view width inside maxWidth by calling parentView.getMeasureWidth(). which is your parent view here? – Nishant Apr 04 '12 at 08:40
  • 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 view – Nik Apr 05 '12 at 07:08
  • @KorniltsevAnatoly Check it out here http://stackoverflow.com/questions/3937238/android-view-measureint-int-always-wants-to-be-0-0 – Nishant Oct 04 '12 at 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 Oct 30 '14 at 10:52