How to set ListView Height to auto? - android

I want to set the ListView height to auto means as per my items will add to listview then at that time height will also increase auto automatically.
<ScrollView
android:id="#+id/abcd_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/profile_layout"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/blur_default_img">
<RelativeLayout
android:id="#+id/profile_img_rt"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:gravity="center"
android:background="#drawable/circle_white_bg">
<com.example.RoundedImageView.RoundedImageView
android:id="#+id/profile_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/person_avatar" />
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="#+id/tab_pending_list"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_below="#+id/profile_layout">
</ListView>
</LinearLayout>
</ScrollView>
so this the xml please kindly go through it and suggest me solution that how to increase the height of the listview automatically.

The ListView is a scrollable view so you should never put it inside another scrollable view like a ScrollView.
The ListView will display only the row describe by the adapter behind it. So all you need to do is to just delete the ScrollView and keep the ListView with android:height=wrap_content

Related

Layout not showing under listview within ScrollView

I have a layout with 1 layout inside of it which is all inside a ScrollView. I am trying to get the whole screen to scroll with the content inside of the listview then the textviews underneath that listview. I get the whole listview to populate and show correctly but the bottom layout with all the textviews do not show up.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_below="#id/app_bar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="#+id/bannerReceiptLogo"
android:layout_width="170dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/img_logo_receipt_cub" />
<TextView
android:id="#+id/bannerAddressHeader"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#id/bannerReceiptLogo"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="#string/storeHeader"
android:textAlignment="center"
android:textSize="18sp" />
<ListView
android:id="#+id/fullEReceiptListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bannerAddressHeader"
android:layout_marginTop="10dp"
android:scrollbars="none"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/fullEReceiptListView"
android:layout_weight="1">
<View
android:id="#+id/totalDivider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#color/colorGrey"
/>
<TextView
android:id="#+id/txtSubTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBTOTAL"
android:textSize="20sp"
android:layout_below="#+id/totalDivider"/>
<TextView
android:id="#+id/txtSubTotalFinal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$13.58"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/totalDivider"/>
<TextView
android:id="#+id/txtTaxText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAX"
android:textSize="20sp"
android:layout_below="#+id/txtSubTotal"/>
<TextView
android:id="#+id/txtTaxTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$0.80"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/txtSubTotalFinal"/>
<TextView
android:id="#+id/txtCompleteTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:textSize="20sp"
android:layout_below="#+id/txtTaxText"/>
<TextView
android:id="#+id/txtCompleteTotalNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$14.38"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/txtTaxText"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Do not put ListView inside ScrollView. ListView is a widget that is already scrollable. See NestedScrollView from Support library.
If you set the ListView height to wrap_content the height will have the height to comport all items. That way, the below content will be pushed away.
However Android don't have a great support for nested scrolls, it's considered a bad practice. This way, your ListView will not have the native scroll feature, and will need some hack to work as expected.
A better workaround is use the NestedScrollView, so you scroll may work (maybe you will need put your ListView inside another parent to control the height)
change ListView's layout_height to wrap_content

Scrolling two listview inside one LinearLayout

I am trying to scroll the listview inside one Linear Layout but it is not working as i want. I am attaching one image for the more explanation.
This is my Layout
<android.support.v7.widget.CardView
android:id="#+id/card"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:clickable="false"
app:cardBackgroundColor="#android:color/white"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical">
<Button
android:id="#+id/findSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:background="#drawable/flat_selector_green"
android:text="Next"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
Image explanation
I know it s not the good approach to use the listview but i have to do this with this way.
Just remove the ScrollView from your xml code, Because ListView have their own Scroll doesn't require ScrollView.Then code will work as you want. Second List will be shown after showing every item of first ListView.
According to android docmentation
You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.
First you user card view as top parent it seem you will use it as child of list view as I guess , if my guessing is correct this not wise to use two list view as child.
Second In any case you want use two listview in same view , its recommended to separate between them with view (TextView , ... etc).
the solution
1- remove scroll as it useless.
2- add weight to your list view as wrap content in your case mean whole view.
3- recommend add view to separate between list.
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:clickable="false"
app:cardBackgroundColor="#android:color/white"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wholeview"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<!-- recommended to add view here -->
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/findSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="Next"
android:textColor="#android:color/white"
/>
</LinearLayout>
</RelativeLayout>

Show 3 scrollable ListViews of equal height in Android Layout

I am trying to show three different vertical sections in my Android layout on one screen, each taking up one third of the screen, one on the top, one in the middle, and one on the bottom. Each section has a TextView and a ListView, and the ListViews can scroll so that you can see all items, but the overall page does not move. I have tried putting each TextView and ListView in a LinearLayout and setting the height of each LinearLayout to one third the total height of the screen, but the first ListView just shows all the items in it, taking up most of the screen, and the other sections are pushed down. I also tried using layout_weights, but for some reason it did not work. (EDIT: Setting layout_weight="1" ended up working, I'm not sure what I did wrong the first time) How can I make this work, or is there a better way to go about this?
<LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#FF0000"/>
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#00FF00">
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#0000FF">
</LinearLayout>
This will give you three equally wide columns: to make it rows, change the orientation to vertical and swap the values of layout_height and layout_width for each listview. If you need to add the TextViews, you'll have to make each listview in this code either a RelativeLayout LinearLayout or FrameLayout, using same width/height/weight and arranging the ListView and TextView inside to taste. To do this most efficiently, use a Framelayout and use a margin on the listview to offset it from the TextView. You can place the TextView relative to the ListView inside the FrameLayout by using the layout_gravity in the TextView.
Ie (swapping the first "column"):
<FrameLayout android:orientation="vertical" android:layout_width="0dp"
android:layout_weight="1" android:layout_height="match_parent"
android:background="#FF0000">
<TextView android:text="Column!" android:background="#3Eff0000"
android:layout_height="40dp" android:layout_width="match_parent">
<ListView android:layout_marginTop="48dp" android:layout_height="match_parent"
android:layout_width="match_parent" android:background="#8Aff0000"/>
</FrameLayout>
Use this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ff89ff91">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#1"
android:id="#+id/textView"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff8177">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#2"
android:id="#+id/textView2"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView2"
android:layout_below="#+id/textView2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffe85d">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#3"
android:id="#+id/textView3"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView3"
android:layout_below="#+id/textView3" />
</RelativeLayout>
</LinearLayout>

ScrollView with LinearLayout inside does not work

I have a scrollView with two inside LinearLayout but it does not work, the listView does not show all items but only a few. How can I do?
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/welcomeView"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/image_User_welcome"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="70"
android:src="#drawable/user" />
<TextView
android:id="#+id/private_area_welcome"
style="#style/CoopLabel"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="30"
android:text="#string/private_area_welcome" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/welcomeView">
<TextView
android:id="#+id/private_area_lists"
style="#style/CoopLabel"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/linearLayout1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:text="#string/private_area_lists" />
<ListView
android:id="#+id/privareaList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:textColor="#000000" >
</ListView>
<ImageButton
android:id="#+id/logoutBtn"
android:layout_width="160dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="0dp"
android:scaleType="centerCrop"
android:src="#drawable/tasto_logout"
/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
It is not recommended to use scrollables containing other scrollables - your layout contains ListView inside Scrollview.
You can design your layout as a ListView - you can add header and footer views(welcomeView, logoutBtn, etc..) to the top and bottom of listview - see methods ListView.addHeaderView and addFooterView) - scrolling will be managed by ListView,
or you can replace listview in your original layout with simple LinearLayout and populate it manually row by row(do not forget to assign click listeners to rows etc..) - scrolling will be managed by ScrollView.
To find more information about this problem, try to google "android listview inside scrollview", i am sure it will help you. :-)

Android: how to make a listview fill its parent without scrolling?

I want my whole activity to scroll at once, but the ListView which is embedded in my ScrollView won't expand. I just see the first row, but I can scroll through the other rows, when scrolling the listView.
What I need is for the listView to expand in height so every items it contains are displayed without having to scroll the listview, only the main scrollView...
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/header_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
...
</LinearLayout>
<TextView
android:id="#+id/comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/gradient_background"
android:text="#string/comments"
android:textColor="#000000"
android:textStyle="bold" >
</TextView>
<ListView
android:id="#+id/commentsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:divider="#drawable/gradient_divider"
android:dividerHeight="1dp" />
</LinearLayout>
</ScrollView>
One of the main purposes of a ListView is to enable recycling of cells while scrolling. If you want all the cells to exist, then you don't need a ListView. Just use a vertically oriented LinearLayout.

Categories

Resources