Manju
Manju

Reputation: 717

android How to add list view in a scrollview

I have a layout requirement like below,

Textview
TextView
ListView
Edit Text
Button

Since listview cannot fit in landscape, I want to have list view onwards (ie. listview, edittext and button) to be a scroll view.

I know listview cannot be used inside a scrollview, but is there a way to do that ? Any working example will be appreciated.

Upvotes: 2

Views: 2961

Answers (4)

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

99% of android developers think we should not use ListView inside a ScrollView because both are scrollbale views and only parent can be scrollable, so it wraps the ListView.

Its 100% correct. But we have to use tricks to avoid this and to achieve our requirements.

I found one trick in web, which is setting the height of ListView based on the list items. Just check the link below, you will get an example code to calculate the height of ListView to fit inside a ScollView.

Android ListView height calculation to fit in ScrollView

The problem with this code is the list view will be filled entire screen if more children are available.

You have to use below template to achieve solution to your requirement.

<ScrollView >
    <LinearLayout vertical>
        <TextView />
        <TextView />
        <ListView />
        <EditText />
        <Button />
    </LinearLayout>
</ScrollView>

I saw one video on youtube, Android ListView inside a ScrollView which is showing we can limit the height of listview, can be scrollable and used inside a ScrollView. I don't know how the programmer achieved that.

I am also thinking to produce same result by avoiding above example code. I hope it may help you temporarily. Please let me know if you got solution.

Upvotes: 1

bluefalcon
bluefalcon

Reputation: 4235

Sort of a round about way to do what you want to do without a scroll view.

  • Write a custom adapter for your ListView
  • Assume you have an array of n elements that you want to populate the ListView with and then the EditText and the Button. So number of elements will be n+2
  • In the getView for the position n+1 return a view which has an EditText box instead of the normal list item
  • For the n+2 position return a Button.

Don't try to wrap around a ListView with a ScrollView, you will need up with lot of issues.

Note: I have not tested this, not even sure if it will work. Do let me know if it works. :)

Upvotes: 0

himanshu
himanshu

Reputation: 1980

The better solution for this kind of layout is that You should use relative layout and fix ur EditText and Button at the bottom of ur screen like i have in my list view(see the image below) so that you wont need to add ScrollView in ur layout.enter image description here

Upvotes: 0

Arslan Anwar
Arslan Anwar

Reputation: 18746

Just do this

<ScrollView>
          <LinearLayout>
                      <ListView>
          </LinearLayout>             
</ScrollView>

Then add your

EditText

Button

Upvotes: 0

Related Questions