ataulm
ataulm

Reputation: 15334

LinearLayout remains width 0 (despite explicitly setting, or implicitly with weights)

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.

Upvotes: 1

Views: 1456

Answers (3)

slund
slund

Reputation: 6397

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?

Upvotes: 1

Malcolm
Malcolm

Reputation: 41510

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.

Upvotes: 0

Femi
Femi

Reputation: 64700

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.

Upvotes: 0

Related Questions