ListView inside a ScrollView issue in Android - android

Here is my XML layout. I have a list view inside my scrollview. I am setting the height manually if I change the listview height to wrap_content it is not working. And also if the data grows beyond the limit it is hiding. I am seeing many answers to solve this. What is the best option to solve it for my case?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusableInTouchMode="true" >
<LinearLayout
android:id="#+id/store_scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#c6c6c6"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout_alignParentLeft="true"
android:text="Stores"
android:textSize="20dp" />
<TextView
android:id="#+id/merchant_count_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="0 Results" />
</RelativeLayout>
<Gallery
android:id="#+id/gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:gravity="left"
android:listSelector="#color/gridviewlistselector"
android:paddingLeft="10dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:spacing="10dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#c6c6c6"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout_alignParentLeft="true"
android:text="Products"
android:textSize="20dp" />
<Spinner
android:id="#+id/filter_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#+id/textView1" />
<TextView
android:id="#+id/product_count_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="0 Results" />
</RelativeLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="4800dp"
android:layout_weight="1"
android:drawSelectorOnTop="true"
android:listSelector="#color/gridviewlistselector"
android:orientation="vertical" />
</LinearLayout>
</LinearLayout>
</ScrollView>

Short answer: You can't (shouldn't) have a ListView wrapped in a ScrollView.
Long answer: As Romain Guy (UI-guru at Google themselves) once said:
Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.
Quote via "How can I put a ListView into a ScrollView without it collapsing?".

Instead using List View in ScrollView you can make separate layout where you can define your desired components. then you can make that layout haeder of the ListView. You can use following code:
LayoutInflater inflater = LayoutInflater.from(this);
View LTop = inflater.inflate(R.layout.header_tolistview, null);
listview.addHeaderView(LTop);

Related

How to convert a GridView to an ExpandableListView

I have used GridView in my project and with that I included the ViewHolder.
Now, the customer wants to show the data using an ExpandableListView for the fact that he wants to only show the header and when the user clicks on the header, it shows the child below it.
Having no prior experience, I want to know if it possible to change from GridView to ExpandableListView easily in the code.
Note that I have also included text search on the Grid. Will that still be available if I switch to ExpandableListView?
If the above is possible, can anyone help how I could achieve it.
The Grid row xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="8dip"
android:paddingRight="4dip">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.8">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:typeface="sans"
android:textSize="13sp"
android:textStyle="bold"
android:id="#+id/placeName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#88FFFFFF"
android:typeface="sans"
android:textSize="13sp"
android:textStyle="bold"
android:id="#+id/theme"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:id="#+id/Title"/>
<TextView
android:id="#+id/Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dip"
/>
<TextView
android:id="#+id/scheduleDays"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_gravity="bottom"
android:layout_marginRight="5dip"
android:layout_marginTop="7dip"
android:text="Family, Friends, Relatives, Grand-Parents"
android:textStyle="bold"/>
<TextView
android:id="#+id/Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_gravity="right"
android:layout_marginRight="5dip"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/rightLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="vertical"
android:minWidth="60dp"
android:layout_weight="0.2">
<ImageView
android:id="#+id/starButton"
android:layout_width="40dp"
android:layout_gravity="right"
android:layout_marginRight="5dip"
android:layout_height="40dp"
android:background="#00FFFFFF"
android:src="#drawable/Favourite"/>
<ImageView
android:id="#+id/showMeButton"
android:layout_width="40dp"
android:layout_gravity="right"
android:layout_marginRight="15dip"
android:src="#drawable/showMe"/>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/separator"
android:layout_marginTop="5dip"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#33000000" />
</LinearLayout>
With ListView, I want to show PlaceName, Favourite Button and ShowMe Button as the headers and the rest as the child.
You can integrate the Expandable ListView in an Layout, like LinearLayout.
For this Change the Layout type in the layout.xml of your activity
To send the Data to the ListView you need to use an BaseExpandableListAdapter that pulls the Content out of an Array or a Database.
Example for the layout.xml
<LinearLayout .... >
<ExpandableListView
.... >
</ExpandableListView>
</LinearLayout>
can you show your xml file. well can be achieve is many ways.
like change your layout container from grid layout to linear layout
as in your adapter
and set your View in onCreate
recycler.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recycler.setLayoutManager(layoutManager);
adapter = new Adapter(getActivity());
recycler.setAdapter(adapter);

Use a vertical line in a ListView item

I'm trying to show a vertical line in an android ListView item. I define it in the XML for the cell but when I preview it in the ListView it doesn't appear.
I understand this is an issue because I see alot of questions on here, but don't see any real answer.
The XML is pretty standard:
Cell:
<?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"
android:background="#color/appWhite" >
<View android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/appGreen"
android:layout_alignParentLeft="true" />
<RelativeLayout
android:id="#+id/cell_trip_info_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/vertical_bar" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cell_trip_name"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:textColor="#2c3e50"
android:paddingLeft="10dp"
android:text="adfadf" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cell_trip_country"
android:layout_below="#+id/cell_trip_name"
android:textColor="#2c3e50"
android:paddingLeft="10dp"
android:text="adfadf" />
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/whole_container"
android:layout_alignParentRight="true" >
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/appGreen"
android:id="#+id/cell_trip_date_box"
android:layout_alignTop="#+id/update_buttons"
android:layout_alignBottom="#+id/update_buttons"
android:padding="20dp" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cell_tripstart_date"
android:layout_centerHorizontal="true"
android:textColor="#fff" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cell_date_thru"
android:text="#string/thru"
android:layout_below="#+id/cell_tripstart_date"
android:layout_centerHorizontal="true"
android:textColor="#fff" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cell_tripend_date"
android:layout_below="#+id/cell_date_thru"
android:textColor="#fff"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/update_buttons"
android:visibility="gone"
android:layout_toRightOf="#+id/cell_trip_date_box"
android:background="#e9e9e9"
android:padding="20dp" >
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/cell_button_edit_trip"
android:src="#drawable/slide_edit"
android:adjustViewBounds="true"
android:contentDescription="#string/content_description" />
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/cell_button_delete_trip"
android:src="#drawable/slide_delete"
android:layout_toRightOf="#+id/cell_button_edit_trip"
android:adjustViewBounds="true"
android:contentDescription="#string/content_description" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
I found the cause and the obvious solution/workaround on another post Here
Basically RelativeLayouts for whatever reason won't play nice with the "match_parent" height setting in a ListView Item. One of the answer to the post recommended using a LinearLayout instead. So I just created a new File with the parent view being a LinearLayout. I then created a RelativeLayout child and tossed everything else in there.
It worked fine this time, easily allowing me to create my vertical rule.
In your cell_trip_info_container RelativeLayout you have
android:layout_toRightOf="#+id/vertical_bar"
You dont have a View named vertical_bar
Add that ID to your vertical bar view and you should be good to go
<View
android:id="#+id/vertical_bar"
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/appGreen"
android:layout_alignParentLeft="true" />
change
RelativeLayout
to
VerticalLayout

adding elements to scrollview using java when there is more then 1 child

I am trying to add elements to my scrollview,
this scroll view has a vertical linear layout inside of it.
(i added another linear layout - horizontl, inside that will manage 2 textviews and an image according to the attributes i assigned them),
i am trying to add elements of this inner layout to my scroll view (id=insideLineLayout)
(each element added to scrollview will contain these 2 texts and an image)
because of the fact scrollview can only have 1 child i am unable to do this.
would appreciate some help, nothing seems to be working here.
Thanks.
my current xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchScreenOuterLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/catagoryselectedText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="selected catagory"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/searchText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp" />
<ImageButton
android:id="#+id/searchButton"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="#drawable/search_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/radiusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="5dp"
android:text="Search Radius"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="#+id/radiusBar"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
<ScrollView
android:id="#+id/searchScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/searchScrollViewLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="10dp" >
<LinearLayout
android:id="#+id/insideLineLayout"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:weightSum="100"
>
<TextView
android:id="#+id/linesPrice"
android:layout_weight="20"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="tmp1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/linesInfo"
android:layout_weight="60"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="tmp2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageInfo"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#drawable/tv_icon" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Consider how long your list can get as ListView has been optimized in many ways which a regular scroll view is not. If you have too many elements you might start get out of memory exceptions.
In order to dynamically add views to a ScrollView you first need to get the container view inside the ScrollView (most likely a LinearLayout), then you simply do following for each view you add:
View view = inflater.inflate(R.layout.view_to_add, container, false); // this is used if you create from xml, if you make it in code you'd use the regular constructor.
// update the view content (view.findViewById(); etc.)
container.addView(view);

List view with image

My problem is to find best view configuration for my application that have a record in the listView.
My main.xml has this content:
<LinearLayout android:id="#+id/tabName" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textConnection" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:minHeight="20dp"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textTitle" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:minHeight="40dp"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listRecord"
android:layout_width="match_parent" android:layout_height="match_parent">
</ListView>
</LinearLayout>
And the my actual row of ListView is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageBox"
android:layout_width="48px"
android:layout_marginTop="13dp"
android:layout_height="48px">
</ImageView>
<TextView
android:id="#+id/longText" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/num1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/num2" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/num3" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
/>
</LinearLayout>
I tried with TableLayout but starting the application report an error in the bindView.
Can I write my XML code with only textView?
Consider that then I must add the caption of the ListView that have three image above each column with number.
Thank's.
I think the ListView is indeed your best option.
Can I write my XML code with only textView?
Yes, of course.
I don't see what is your problem actually. You should have an activity layout with the textConnection, the textTitle, then the 3 images aligned to the right (missing in your layout file), and the listview.
As for the row xml layout, I think it is OK! Just combine it with a simple cursor adapter in your java code, and you're done.

problem to make scroll able,set scroll for whole layout android

had make layout for showing the content in listview and a button below on list view for displaying more information on list view ,so first i am showing only 4 rows in list view and when i next time click on button 4 rows again added in list so now my button goes down in screen so how to make whole layout scroll able so that i can go to button via scrolling my layout is..
listplacehold.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1A77BD"
>
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<View
android:id="#+id/horizontolline"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_below="#id/android:list"
android:background="#000000"
/>
<Button
android:text="Load More..."
android:textColor="#color/white"
android:id="#+id/keywordsearchbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/horizontolline"
android:drawableLeft="#drawable/load_more_off"
android:gravity="left|center_vertical"
android:background="#1A77BD"
/>
</RelativeLayout>
and for list item layout is: listitem.xml is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<!-- Location where barrio items will be displayed -->
<TextView
android:id="#+id/reportid"
android:layout_marginLeft="70dip"
android:text="reporet id"
android:textColor="#color/white"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView android:id="#+id/status"
android:textColor="#color/white"
android:text="status"
android:layout_marginTop="40dip"
android:layout_marginLeft="200dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/title"
android:text="title"
android:layout_marginLeft="150dip"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/date"
android:text="dateeeeeee"
android:layout_marginLeft="70dip"
android:layout_marginTop="40dip"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
items are dynamically loadein list view when button click
i think that scroll will be set on above xml means on "listplacehold.xml" but how?,when i am doing it is set to for half screen,means it is not displaying in whole view... Thanks for help in advance.
don't use the ScrollView with the combination of the ListView. Instead make your layout like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1A77BD" >
<Button
android:id="#+id/lodmore"
android:text="Load More..."
android:textColor="#color/white"
android:id="#+id/keywordsearchbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true""
android:drawableLeft="#drawable/load_more_off"
android:gravity="left|center_vertical"
android:background="#1A77BD"/>
<View
android:id="#+id/horizontolline"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_above="#id/loadmore"
android:background="#000000"/>
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_above="#id/horizontolline/>
</RelativeLayout>
put scrollview over your root layout and remember only one layout node is allowed under scrollview.....
refer this doc:
http://developer.android.com/reference/android/widget/ScrollView.html

Categories

Resources