Home » Android » android – Multiple ListViews inside a ScrollView

android – Multiple ListViews inside a ScrollView

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a complex xml file and I really need a ScrollView. Please Don’t mind the Ids as I have changed them. The ScrollView here doesn’t work.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/f_summary_LL">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/BT1" 
android:textColor="@color/white"
android:textStyle="bold"         
android:gravity="center_vertical|left" 
android:paddingLeft="2dp" 
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/BTS1" 
android:textColor="@color/white"    
android:paddingLeft="4dp"
android:textSize="13sp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"  
android:gravity="center_vertical|right"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView7"
android:textSize="13sp"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/BT1CRR" android:textColor="@color/white" 
android:paddingRight="8dp" android:textSize="13sp"
android:paddingLeft="2dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView" android:textColor="@color/white"
android:layout_weight="3" android:gravity="center"
android:background="@drawable/table_header_cell_first" 
android:textStyle="bold"
android:textSize="13sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="R"
android:id="@+id/textView1" android:textColor="@color/white" 
android:gravity="center" android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold" android:layout_weight="1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="B"
android:id="@+id/textView2" android:textColor="@color/white" 
android:gravity="center" android:background="@drawable/table_header_cell"
android:textStyle="bold" android:textSize="13sp"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="4s"
android:id="@+id/textView3" android:textColor="@color/white" 
android:gravity="center" android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="6s"
android:id="@+id/textView4" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="SR"
android:id="@+id/textView5" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:baselineAligned="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:baselineAligned="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/BT2" android:textStyle="bold" 
android:textColor="@color/white"
android:gravity="center_vertical|left" android:paddingLeft="2dp" 
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/BTS2" android:textSize="13sp" 
android:textColor="@color/white"
android:paddingLeft="4dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textViewBH" android:textColor="@color/white"
android:layout_weight="3" android:gravity="center"
android:background="@drawable/table_header_cell_first" 
android:textSize="13sp"
android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="O"
android:id="@+id/textViewBOH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold" android:clickable="false"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="M"
android:id="@+id/textViewBMH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" android:background="@drawable/table_header_cell"
android:textStyle="bold" android:textSize="13sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="R"
android:id="@+id/textViewBRH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="W"
android:id="@+id/textViewBWH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textViewBECOH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView2"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/innings2">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/BT2" android:textStyle="bold" 
android:textColor="@color/white"
android:gravity="center_vertical|left" android:paddingLeft="2dp" 
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/BTS2" android:textSize="13sp" 
android:textColor="@color/white"
android:paddingLeft="4dp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"  
android:gravity="center_vertical|right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView7" android:textStyle="bold" 
android:textColor="@color/white"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:id="@+id/Bae2C" android:textColor="@color/white" 
android:paddingLeft="2dp"
android:paddingRight="8dp" android:textSize="13sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView" android:textColor="@color/white"
android:layout_weight="3" android:gravity="center"
android:background="@drawable/table_header_cell_first" 
android:textSize="13sp"
android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="R"
android:id="@+id/textView9" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold" 
android:clickable="false"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView10" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textStyle="bold" android:textSize="13sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView11" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView4" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView5" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView3"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:baselineAligned="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:baselineAligned="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/Bw1" android:textStyle="bold" 
android:textColor="@color/white"
android:gravity="center_vertical|left" android:paddingLeft="2dp" 
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/BoSe1" android:textSize="13sp" 
android:textColor="@color/white"
android:paddingLeft="4dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textViewBH" android:textColor="@color/white"
android:layout_weight="3" android:gravity="center"
android:background="@drawable/table_header_cell_first" 
android:textSize="13sp"
android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textViewBOH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold" 
android:clickable="false"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textViewBMH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textStyle="bold" android:textSize="13sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textViewBRH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textViewBWsH" android:textColor="@color/white"  
android:layout_weight="1"               
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textViewBECOH" android:textColor="@color/white" 
android:layout_weight="1"
android:gravity="center" 
android:background="@drawable/table_header_cell"
android:textSize="13sp" android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView4"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
How to&Answers:

It can be done, although you should not put a listview inside a scrollview but sometimes this is the easier way: https://stackoverflow.com/a/3495908/1117338

Answer:

Answer from a Google employee that works in Android:

Do not use ListView inside ScrollView

https://plus.google.com/107708120842840792570/posts/T5AgJ7jiK88

enter image description here

Answer:

Use Like this: THIS WORKED LIKE CHARM

Remove Linear layout. use relative layout and inside that place your two list view like this.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollojt" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#f00"></ListView> <ListView android:id="@+id/listView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/listView1" android:background="#0f0"></ListView> </RelativeLayout> </ScrollView> 

add Utility.java

public class Utility { public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); } } 

In your Activity:

lv1.setAdapter(adapter); lv2.setAdapter(adapter); Utility.setListViewHeightBasedOnChildren(lv1); Utility.setListViewHeightBasedOnChildren(lv2); 

Thanks to #Nirmal for the actual answer https://stackoverflow.com/a/17693628/1403112

Answer:

ScrollView and ListView both use scrolling. So in my opinion they shouldn’t be used together. Try using RelativeLayout or some other layout instead of ScrollView

Answer:

You can set it by engulfing ListView in a parent layout like an example below –

<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.cardview.widget.CardView android:id="@+id/cardBrand" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/fab_margin" android:layout_below="@+id/cardRange" app:cardElevation="3dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/brandTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Brands" android:layout_marginTop="@dimen/fab_margin" android:layout_marginLeft="@dimen/fab_margin" android:layout_marginRight="@dimen/fab_margin" android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textColor="@color/md_black_1000"/> <ListView android:id="@+id/brandList" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="@dimen/fab_margin" /> </LinearLayout> </androidx.cardview.widget.CardView> <Space android:layout_width="match_parent" android:layout_height="6dp" /> <androidx.cardview.widget.CardView android:id="@+id/cardRating" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/fab_margin" android:layout_below="@+id/cardBrand" app:cardElevation="3dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Ratings" android:layout_marginTop="@dimen/fab_margin" android:layout_marginLeft="@dimen/fab_margin" android:layout_marginRight="@dimen/fab_margin" android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textColor="@color/md_black_1000"/> <ListView android:id="@+id/ratingList" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="@dimen/fab_margin" /> </LinearLayout> </androidx.cardview.widget.CardView> <Space android:layout_width="match_parent" android:layout_height="6dp" /> </LinearLayout> </androidx.core.widget.NestedScrollView> 

And, after the listview is populated –

eachitemSize = 180; cardBrand.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, eachitemSize * brandsAdapter.getCount())); cardRating.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, eachitemSize * ratingAdapter.getCount())); 

What we did above is, we set the ScrollView height to match_parent. And after the ListView is populated we defined each parent layout of ListView to its ListView item count. And, remember to change the value of eachitemSize according to your each cell size.

Another thing to care about is after the LayoutParams is assigned its margin becomes invalid due to the new LayoutParams. So, use <Space/> instead of margin like the example above.

Hope it helps!!