1

With the below layout (a portion of a larger layout), the container layout's visibility is set to "gone" until a button is clicked. On the button click, if the container_ll is not visible, it's set to visible, and a custom view is added to the reminderViews_ll container.

The views are being added, and the container_ll view is visible on button click. What follows is the width and height of various views after the button is clicked.

container_ll         width 420, height 96.  
lineDivider_view     width 420, height 2 (as expected)  
reminder_img         width 36, height 36 (as expected, hdpi phone)  
reminderViews_ll     width 0, height 96 (argh)


<LinearLayout
    android:id="@+id/container_ll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:visibility="gone"                   
    >

    <View style="@style/lineDivider_view" />

    <ImageView
        android:id="@+id/reminder_img"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center_horizontal"  
        />

    <!-- Stick the actual Reminder TVs + Del buttons in here dynamically -->
    <LinearLayout
        android:id="@+id/reminderViews_ll"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        />                  
</LinearLayout>

I'm at a bit of a loss as to where to go from here. I was thinking invalidate layout, to force it to draw again after making the view visible, but that's never worked for me (seemingly), and if the reminderViews_ll can get a height of 96, then it can't be an issue with when it's calculating the dimensions of the LinearLayout.

I hope you have enjoyed reading this question as much as I have writing it. Any pointers, as always, appreciated.

ataulm
  • 15,195
  • 7
  • 50
  • 92
  • For those asking why I've set 0dp for the width, please see the following question and answer to show I'm not being crazy :D --http://stackoverflow.com/questions/3470420/is-it-possible-to-evenly-distribute-buttons-across-the-width-of-an-android-linear – ataulm May 04 '11 at 22:01
  • Does the container expand and the buttons show if you set them in the XML (inside reminderViews_ll)? If so, then there might be something wrong with the "dynamic" code. – dmon May 04 '11 at 23:57

3 Answers3

1

The numbers you show look correct to me.

The container is width 420. The other views that are set to fill_parent or wrap_content take up all of the space (actually more). The Linear layout then goes to carve up the remaining space to any weighted views. Since there is no space to allocate, you get zero.

container_ll         width 420  
lineDivider_view      - 420  
reminder_img          - 36 
      =               -36

so this makes sense
reminderViews_ll     width 0

There simply is no room to give it.

Why are you using a horizontal line divider in your horizontal view?

slund
  • 6,367
  • 2
  • 26
  • 19
  • man What a dumbo... I am. The horizontal line divider was meant to go outside at which point it should work, I'll give it a go when I get to work, but marking as answer as this is it. – ataulm May 05 '11 at 07:41
0

Ah, very confused: layout_width="fill_parent" and layout_weight="1.0" doesn't work?

I mean, layout_width="0dp" is guaranteed to be width 0, regardless of what you put into it, so that one will never do what you want. If you are using fill_parent and its still not working I'd question if you are adding your custom View into the right LinearLayout, because that really should work correctly.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I've used layout_width="0dp" in combination with layout_weight="1.0" and layout_height="wrap_content"/"fill_parent", where the aim is achieved, in that it will fill the remaining space width-ways (assuming nothing else has been given a weight in the same section-thingy). I used hierarchy viewer to check the heights/widths, and indeed that the views where being added to the correct containers. :s – ataulm May 04 '11 at 21:50
  • no, this is not true; by setting the width of 1 of the views, and the other to 0dp + layout_weight=1.0, it will allow the second 0dp view to fill the remaining space after ensuring the first view has the width it asked for. Paranoid as I am :P, I tested it on a new project with three TextViews, each with a different background color: 25dp, 50dp and 0dp+layout_weight=1.0 – ataulm May 04 '11 at 22:23
  • Interesting. I stand corrected: didn't realize you could even do that. – Femi May 04 '11 at 22:24
  • neither did I for ages, it's awesome :D – ataulm May 04 '11 at 22:43
  • Well, at the risk of saying it, it could very well be a bug in the behavior of LinearLayout if it works everywhere else but falls over in this one instance. – Femi May 04 '11 at 22:45
  • I totally hope not. Though it would be a good reason to er.. start reading about Relative Layouts :D – ataulm May 04 '11 at 22:46
0

I see that you've set android:layout_width for reminderViews_ll to 0 dp, you have to change this parameter, possibly dynamically if you want to. I don't really understand why you set it to 0 dp and then ask why it has zero width.

Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • Even with fill_parent it produces the same result. The combination of width=0dp and weight=1.0 should expand the view to fill the remaining space in its container. – ataulm May 04 '11 at 22:00