Due popularity, I have decided to create a GitHub repo for this project. Please checkout the code at: https://github.com/dinocore1/DevsmartLib-Android Pull requests are welcome!
Licensed under MIT License Copyright (c) 2011 Paul Soucy, (paul+blog@dev-smart.com)
The Android API seems to be lacking a Horizontal ListView widget. Basically, what I needed was something exactly like the Gallery widget but without the center-locking feature. After much googling (and finding dead ends like this or this), I eventually came to the conclusion that a Horizontal ListView simply did not exist. So I built my own…
My Android Horizontal ListView implementation has the following features:
- Subclass AdapterView so I can make use of adapters
- Fast – make use of recycled views when possible
- Items are clickable – (accepts AdapterView.OnItemClickListener)
- Scrollable
- No center-locking
- Simple – is that so much to ask?
If you find this helpful, let me know, I would love to hear your feedback.
How to use:
Horizontal ListView is ment to be a drop-in replacement for a standard ListView. Here is some quick demo code to get you started:
package com.devsmart.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.devsmart.android.ui.HorizontialListView;
public class HorizontalListViewDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
HorizontialListView listview = (HorizontialListView) findViewById(R.id.listview);
listview.setAdapter(mAdapter);
}
private static String[] dataObjects = new String[]{ "Text #1",
"Text #2",
"Text #3" };
private BaseAdapter mAdapter = new BaseAdapter() {
@Override
public int getCount() {
return dataObjects.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
TextView title = (TextView) retval.findViewById(R.id.title);
title.setText(dataObjects[position]);
return retval;
}
};
}
res/layout/listviewdemo.xml:
res/layout/listitem.xml:
Download code from GitHub
old download link: Horizontal Listview (8423)
Changelist
1.5:
adapter.notifyDataSetChanged() now saves position in list instead of starting at begining
1.4:
Added code to respond to adapter.notifyDataSetChanged()
1.3:
added mScroller.forceFinished(true); to the onDown function of mOnGesture so the user con stop the scroll on tap.
Did you find this information helpful? If you did, consider donating.

Hi,
This tutorial is nice and it is exactly what i needed. Thanx a lot. Is there a way to bring snap factor to the same code?
if we could bring the snapping factor back it will be awesome…
Hello,
i use your implementation and i have some problems, i want to scroll from bottom, so i change the position with scrollTo method but it’s not work for me:
HorizontialListView listview = (HorizontialListView) findViewById(R.id.listview);
listview.setAdapter(mAdapter);
listview.scrollTo(getCount()-1);
mAdapter.notifyDataSetChanged();
This doesn’t have scroll listner(onScrollListener),normally its comes with listview.
i need a HorizontalListView with scroll listnering (onScrollListener),can u help me ?
I like this good idea,because give me help!
I am using a customized view , but it is not being displayed, views are getting created but not displayed whereas the imageview is getting displayed.i am using https://github.com/jasonpolites/gesture-imageview.
nice effort

btw i have tried your horizontal list but I need to populate horizontal list view in a vertical list view and I have made two adapters for that one is for populating listview and another one is for horizontal listview but unable to get any view
any Idea how can I achieve my goal
I had the same issue, and had to eventually force a height onto the HorizontalListView to get it to show, my items where exactly 100dp tall, so instead of wrap_content, set it to 100dp.
This is a very useful option/feature whose absence is limiting the HLVs usage very much. Waiting or some useful tips.
Yes,I tried it, I must force a fixed height instead of wrap_content when using tow HorizontalListView in one layout. What I should do if I want to use the “wrap_content”
finally i have succesfully inflated your horizontal list view in List view … but the horizontal scroll is not very much smooth
can you help me further when i scroll horizontal list it will move upward too .. how can i reseolve this issue
HOW DID YOU INFLATE IT? Struggling
I have the view working shown as above. but how do i go on about if i want each item to be sized to fit screen so only one item is show at a time.i tried several variations of fill_parent and match_parent none worked. so please help
Hi,
Nice work, but I have tried your horizontal list but I have a problem, with custom xml item, custom adapter, and custom image downloader (cache), the images lost reference and I have to double click to display them. With native vertical container, this problem does not happen.
Image Downloader link: http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
Thanks for your help!
hi,i found a bug,i use it in a gridview,and the gridview is in fragment,it don’s not work.do you hava try?
Please fix the capitalization issues in the example you’ve provided. This is quite lovely code.
Hi, nice helpfull post but I have a question.
How to implement vertical scroll on items in this horizontalListView?
[...] Please follow following link to have HSV in ListView: http://www.dev-smart.com/archives/34 [...]
[...] This Link.its what you want to [...]
Hi, nice helpfull post but I have a question.
I compare it to ListView ,I find a question that Horizonl ListView doesnot implement onKeyDown(envent) and how to implement it?
Is there a way to get it work inside a scrollview?
How change the height of Horizontal ListView? in the method addAndMeasureChild ?¿?
[...] my app I used two external libraries (Sliding menu and Horizontal Listview). The first one implements a sliding menu effect like the Facebook app and it works fine, I can [...]
Hi,
Thank you very much for your library. I’m using your lib and have a question. My list view item is a image with a checkbox on it. I can check it already but when I scroll out and scroll again, the state of checkbox is uncheck. May be because the recycle view. How can I solve it? Is there any link about this?
I can’t even make the checkbox clickable .. can you help me with that ?
Awesome work!! Thanks
I wanna combine this Horizontal Listview with Regural Listview..
(Horizontal Listview inside each Listview items’)
??
Please anlightened me..
(
[...] am working on one example on horizontal list view . Just modified the package name as package [...]
[...] am working on one example on horizontal list view . Just modified the package name as package com.example.horizaontal; and [...]
[...] here: http://www.dev-smart.com/archives/34. [...]
It’s pretty usefull lib but sometimes it’s scrolling is stuck in the middle of the horizontal list view evenif there lot more items after that.Please help.
Regards,Green
+1 same issue for me. When I change my orientation to landscape mode and back to portrait mode, my adapter does not scroll
what i’m seeing is that the onLayout method is incorrectly saving the mNext and mMax values. After orienting my device back to portrait mode, it’s running into mNext >= mMax every time
I’m trying to use this lib but i got a problem related to onScrollListener. I want to add more item when user scroll to the end of the list but i found out that this lib doesn’t support onScrollListener.
Anybody can help me?
i got the same problem.i want to scroll the list when i pressed a button.but i can’t find any way to do it . if i add an view.scrollTo(x,y),it looks like scrolled.but after that ,the item click position is wrong ,and the list scrolling seems like stuck,how can i fix it?
I m trying to use this lib. Any idea as to how to add a white Horizontal ListView Separator. I tried doing in android:divider=”#FFCC00″ android:dividerHeight=”1px” in listviewdemo.xml. But it doesnt work. Any help will be appreciated.
i’ve provided a temporary fix by adding the following line to onLayout (for those that are having issues with orientation changes):
if (mCurrentX == 0 && mNextX == 0 && mMaxX == 0)
mMaxX = Integer.MAX_VALUE;
cheers m8
Thanks for sharing
Remarkable things here. I am very satisfied to look your article. Thanks so much and I am taking a look forward to touch you. Will you kindly drop me a mail?
[...] Try the following sample code http://www.dev-smart.com/archives/34 [...]
Hi,
I’m using your HorizontalListView inside a standard ListView (if anyone is still strugging – set fixed height of HorizontalListView to see anything
).
I have one small problem – I was trying to get preview of combined lists in Eclipse ADT using xmlns:tools=”http://schemas.android.com/tools” and tools:listitem=”@layout/viewitem”, but HorizontalListView have hard time displaying itself in there.
Maybe someone knows, what needs to be added for it to display correctly? (I already figured out, that adding
if (!this.isInEditMode()) {
mGesture = new GestureDetector(getContext(), mOnGesture);
}
in HorizontalListView’s initView() method removes the “com.devsmart.android.ui.HorizontialListView failed to instantiate.” errors, but what do I need to do next to get preview of the items?
Best regards,
Damian
[...] Horizontal Scroll in ListView [...]
[...] have created horizontal listView as described in http://www.dev-smart.com/archives/34 .Now i need a toast msg or Log message showing the item in the list user clicked. OnItemClick is [...]
[...] I am currently creating a basic news aggregator app for Android, I have so far managed to create multiple HorizontalListViews derived from this: http://www.dev-smart.com/archives/34 [...]
[...] am using this library, but sometime scrolling is not that much [...]
This is nice work. And i have now word for your appreciatation,But i would be happy if android launch such ListView on their own.
[...] am trying to display multiple HorizontalListView in a linearlayout. However, I can only see the firs HorizontalListView. All stuff after that [...]
horizontal listview is not available in android!! how could you used it?
[...] trying to create a common listviews inside a horizontal listview. The horizontal scroll is fine, but the vertical scroll is not working. I’ve tried to use [...]
How HorizontalListView is different from HorizontalScrollView.?
Sorry if its a silly question, but im new to android, and have only started it.
You have to preload all items into HorizontalScrollView. HorizontalListView will load items on demand with an adapter therefore optimizing memory usage.
Thanks for the code, it works brilliantly, just wondering if anyone can help, I would like to add different images. At the moment the code is only displaying the same image 3 times.
Also is there any way that I can get the small little dots at the bottom of the images to scroll through the images.
Hi Paul,
I’m working with your HorizontalListView class. I’m trying to center the items with gravity but I can’t find the way. Every item is a RelativeLayout.
Can you help me, please?
Hi Francesc,
I am having the same issue as well. Were you able to find a solution for this ? All my items are right aligned … So if i have only one or two items in the list, it looks really bad as it is right aligned.
Prashanth
I am using this widget but the list item is taking up the whole width of the screen instead of the defined size in layout. Please help to resolve this issue.
Having the exact same issue. Did you manage to resolve this?
[...] a HorizontalListView in my app and as there is no such existent widget in android sdk, I am using this one instead of building one on my own. But the problem is the list item is not maintaining its size [...]
Hi, have anyone else noticed lags during scrooling? I thing it is due to requesting layout on each scrools step which is wrong. You should only invalidate instead and and move removing views/filling into compute scrool method.
Have you implemented a fix to this problem and tested it out?
HI, I want to add a Header to the List with this type of component , how this can be possible ?
Hi, it’s a useful lib for me, thanks for sharing!!
I’m writing a APP which will publish in google play,
Is it okay to use your horizontal listview in my APP?
Yes, Horizontal list view (and the entire Devsmart Android Lib) is Licensed under the MIT (read more about the license: http://opensource.org/licenses/MIT)
Thanks for the great library. no matter what the size of icons i use , the images stretch and look ugly.how do i adjust the height of the HSV and the icons in it.
Hi. Thanks for your code
By the way, I have a question.
I made a horizonatal listview with your code.
I made width of each item is fill_parent.
I want to made this scroll one by one just like pager. is there any suggestion?
i’ve already tried onTouch method but it doesn’t work perfectly.
Thanks.
The scrolling is not smooth
Change “android:scaletype” to “android:scaleType”. It need a capital letter!
[...] inside of ListViewReviewed by on Mar 3Rating: I’m trying to add a HorizontalListView (www.dev-smart.com/archives/34) to a ListView. The ListView uses an adapter to dynamically attach new HorizontalListView rows for [...]
[...] I am used this. [...]
What if instead of using String[] dataObjects, i use data from parsed JSONArray.
How can i do that one?
You understand thus significantly relating to this subject, produced me in my opinion imagine it from so many varied angles. Its like women and men don’t seem to be involved until it’s one thing to do with Lady gaga! Your own stuffs nice. At all times take care of it up!
Hi,
I am using ur Horizontal listview in my app. In my app there is a view slider like the facebook. when i click a button the view slides. this view contains the horizontal listview, when i scroll the horizontal listview the entire view start scroll from the starting position. is there any way to stop scrolling or disable entire horizontal listview?
Great tutorial.
How can i add the bouncing effect if it reached the end of the list?
Also the snapping?
Hi,
First of all thank you for your great library. My problem is that I am not able to make the listview vertically centered. Would you please explain how can I do that?
Thanks in advance.
hi hazir,
This may help you. try this, use this horizontallistview inside a linearlayout and add this line to your linearlayout.
android:gravity=”center”.
Hi, I’ve tried your solution but it does not work! My xml code is:
Items are all top aligned, so they are not centered at all!
What can I do?
Hi, I don’t know why my XML code does not show. You can see my code in this link:
https://docs.google.com/document/d/1Z8m0qdqo9X_0W9c4dDN0uzw-GePeK_Dw0nkksWOepW4/pub
[...] The Android SDK does not provide a horizontally scrolling ListView. You can use an open source HorizontalListView. [...]
I want each list item to has its own height and I want width of each item to be (screen width – 20dp).. Also, I want divider between each list item.. I spent couple of hours yesterday, but I could not do this.. Can you help me, how to do this!?
[...] Horizontallistview [...]
[...] to create a custom preference for my project. More specifically it is a preference with a HorizontalListView attached directly underneath it. I basically created it by modifying this code for a [...]
Hi it’s me, I am also visiting this web page daily, this website is genuinely fastidious and the viewers are really sharing pleasant thoughts.
[...] -http://www.dev-smart.com/archives/34 [...]
[...] using this Horizontal Listview here and i need to add a layout at the end of the list … it does not support [...]
[...] Horizontal List View .. Load More Leave a reply I’m using this Horizontal Listview here and i need to add a layout at the end of the list … it does not support addFooter() so [...]
Hello everyone. I manage finally to make the wrap content work. This is the code missing:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int firstChildHeight = 0;
int size = getChildCount();
if (size != 0)
{
for (int i = 0; i < size; ++i)
{
final View child = getChildAt(i);
if (i == 0)
{
firstChildHeight = child.getMeasuredHeight();
}
}
setMeasuredDimension(widthMeasureSpec, firstChildHeight);
}
}
Let me know if it works for you and if you have any comments about it. Thanks!
By the way, this should be added in the class HorizontalListView
Thanks mate. It’s working well for me and is nice approach. I have some comment about your code.
If you’re just going to measure height by first child item, I think you don’t need to check whole child, which means you don’t need for-loop.
View child = getChildAt(0); // it will return null if the position does not exist.
if(child != null){
setMeasuredDimension(widthMeasureSpec, child.getMeasuredHeight());
}
Or if you thought about set height by tallest child’s height, you’d better compare each child’s height in the for-loop.
int childHeight = child.getMeasuredHeight();
if(firstChildHeight < childHeight){
firstChildHeight = childHeight;
}
p.s: I still see list view occupying whole height in xml when I design layout. Do you have any idea about this?
Thanks so much for this!
I wasn’t able to get my horizontal listview to center horizontally in its parent (it was always filling the whole screen and filling from the left), so I just added a line to your code here to measure each child’s width, add them up and set the measured width and it worked great!
Thank you,this code is best! But,i have some problems. i want only horizontallistview.how can i do that? thanks in advance. i’m beginner in android
I’m assuming you only want to use the HorizontalListView class and cannot find it in the git repository given the link above. If so, it’s in devsmartlib/src/com/devsmart/android/ui
https://github.com/dinocore1/DevsmartLib-Android/blob/master/devsmartlib/src/com/devsmart/android/ui/HorizontalListView.java
What do you mean exactly that you only want to use horizontallistview?
Even better, Check out this code done by inazaruk https://github.com/inazaruk/android-dev-smart-lib/blob/65d68269d5f453d9b590a02369a04add96ae1d9c/devsmartlib/src/com/devsmart/android/ui/HorizontalListView.java. Now works even when it is in a vertical scrollview. I would just add one last line of code in the method onScroll:
/*
* Now’s a good time to tell our parent to stop intercepting our events! The user has moved more than the
* slop amount, since GestureDetector ensures this before calling this method. Also, if a parent is more
* interested in this touch’s events than we are, it would have intercepted them by now (for example, we can
* assume when a Gallery is in the ListView, a vertical scroll would not end up in this method since a
* ListView would have intercepted it by now).
*/
getParent().requestDisallowInterceptTouchEvent(true);
Hope it helped.
+1!
For whose facing right aligned problem, do the follow fix:
Change:
mScroller.fling(mNextX, 0, (int)-velocityX, 0, 0, mMaxX, 0, 0);
To:
//mScroller.fling(mNextX, 0, (int)-velocityX, 0, 0, mMaxX, 0, 0);
Change:
if(mNextX mMaxX) {
mNextX = mMaxX;
mScroller.forceFinished(true);
}
To:
if(mNextX mMaxX) {
mNextX = mMaxX;
mScroller.forceFinished(true);
}
}
One more change:
At the end of function “fillListRight”
Add:
if (mMaxX < 0)
mMaxX = 0;
[...] this link, you need to customize the [...]
Thanks for the widget. It was necessary.
But I think the “notifyDataSetChanged” is facing some problems: the thing is that, when being called, is removing and re-creating all views in the adapter. This, in large set of items, can lead to memory leaks with subsequent out of memory errors.
This is, in fact, what was happening to me. I tested it using the common ListView, and this leaks are not happening, since the views aren’t re-created when is not needed. A small change in one of the views, means that all getView are called, and being worst, with convertView = null, so a new view is created for each item.
Has anyone faced this problem and found a solution? I am still trying to modify the code to manage the notifyDataSetChanged like ListView does it but it does not seem to be trivial.
Thanks.
I have multiple HorizontalScrollViews inside a ScrollView. Horizontal scroll isn’t smooth at all. I have to scroll almost perfectly horizontally for scrolling to work.
Link to screen: http://i.stack.imgur.com/DcFWE.png
How can I disable vertical scrolling when scrolling horizontally?
Or is there any other solution to this problem?
Thanks.
Hi,
Thank you for this great widget! It sure was needed!
I have a problem that occurs from time to time: When I scroll very fast through my HorizontalListView (swipe once or twice very hard on the screen), it scrolls through almost all the list but it stops at the end, two or three items before the end of the list and gets stuck there. I can scroll again to the left but I can’t seem to get it to scroll further to the right, to the end of the list, past this “stuck point”.
Sometimes the stuck point isn’t even the right border of one of the items, but somewhere in the middle of an item, and it appears as if the HorizontalListView cuts the right side of this stuck item.
Has anyone come across such a problem?
Thank you
how can i add bouncing effect in my horizontal list view using this?
Thank you, I’ve just been looking for info approximately this subject for a long time and yours is the greatest I have came upon so far. However, what in regards to the conclusion? Are you positive about the supply?
Hi,
I have one big issue with the HorizontalListView, It is occupying the complete layout height even though i specify layout_height as match_parent. As a result i find difficulty in using this in smaller devices
Here is my main layout
You can see i have specified layout_height of com.devsmart.android.ui.horizontiallistview as wrap_content. But, the TextView which i have specified after the HorizontalListView is not displaying …. Please help me in solving this issue
NOTE: If i fix the height of the HorizontalListView then it is working fine ..
Hi, Sorry i didn’t mentioned the layout … here is my layout
here is my layout http://pastebin.com/S0RTYq6w
the views are not getting updated after being added as a list item. Suppose i change the height of a layout in the list item it doesnt shows up ??
For hottest news you have to pay a visit internet and on internet I found this web site as a finest web site for
most recent updates.
i want drag and drop functionality in horizontal listview in my app.so can anyone help me to implement drag and drop functionality in android horizontal listview.?? Thanks in advance.
Good way of telling, and fastidious article to obtain data
about my presentation subject, which i am going to convey in school.
Great Work… loved it!
I am now trying to change the position of the list (move to a huge number), like I could do with a normal listview using setSelection(100000). There is no implementation yet. Also scrollTo() is not applicable since android would be lost drawing views and never reaching the target position. Any idea?
An alternative Horziontal ListView library: https://github.com/sephiroth74/HorizontalVariableListView
Pretty! This was an incredibly wonderful article. Thanks for supplying this information.