Search code examples
javaandroidandroid-listview

Can i extend the size of a listview dynamically in java code


I am using a listview in a layout with a size- not in full size of layout. Below the layout there are components like button. I need the layout to get extended when more number of listitems adds dynamically.

I have kept the listview and buttons in scrollview.

Is there any attribute or option to make listview of variable length


Solution

  • You should not have a listview inside a scrollview. Instead of having a listview you should add your items to a linearlayout, which will grow to accomodate the items and not enable the scrollbar within itself. This will solve your UI issue. You can set the onclickListeners in a for looop.

    Update

    <ScrollView.....>
        <LinearLayout .....>
           <LinearLayout ..../> // this has your list items
           <Button .... /> // you can have a layout here if you have multiple buttons.
        </LinearLayout>
    </ScrollView>