Tuesday 13 November 2012

Android horizontal listview | horizontal listview | android listview | android custom horizontal listview

Hello Droid Guys,

In android , there is ListView tag in xml, with the help of this we can show
list view but its apperance is vertically not horizontally and if you want to show
a horizontal listview then you have to make your custom view,


1. Create a new project in Eclipse File New ⇒ Android ⇒ Application Project and fill the required details.
2. Create required files needed to generate a list view. I am using  activity_horizontal_list_view.xml as list view and created a new xml file for single list item named  listview_item.xml

horizontal list view example
Horizontal listView

activity_horizontal_list_view.xml 

<?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="@android:color/white"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/selected_imageview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/gallery_relative_layout"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="30dip"
        android:layout_marginTop="30dip"
        android:background="@drawable/natureimage1" />
    <RelativeLayout
        android:id="@+id/gallery_relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="150dip"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >
        <com.mukesh.ui.HorizontalView
            android:id="@+id/gallery"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginTop="15dp"
            android:smoothScrollbar="true"
            android:spacing="20dip" >
        </com.mukesh.ui.HorizontalView>
    </RelativeLayout>
</RelativeLayout>
listview_item.xml

<?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:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="72dp"
        android:layout_height="75dp" />

</RelativeLayout>





3. HorizontalView.Java : These class help us to create view. This is my custom 
               view class which help us in showing listview Horizontally.
     

package com.mukesh.ui;

import java.util.LinkedList;
import java.util.Queue;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.Scroller;

public class HorizontalView extends AdapterView {

 public boolean mAlwaysOverrideTouch = true;
 protected ListAdapter mAdapter;
 private int mLeftViewIndex = -1;
 private int mRightViewIndex = 0;
 protected int mCurrentX;
 protected int mNextX;
 private int mMaxX = Integer.MAX_VALUE;
 private int mDisplayOffset = 0;
 protected Scroller mScroller;
 private GestureDetector mGesture;
 private Queue mRemovedViewQueue = new LinkedList();
 private OnItemSelectedListener mOnItemSelected;
 private OnItemClickListener mOnItemClicked;
 private OnItemLongClickListener mOnItemLongClicked;
 private boolean mDataChanged = false;

 public HorizontalView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initView();
 }

 private synchronized void initView() {
  mLeftViewIndex = -1;
  mRightViewIndex = 0;
  mDisplayOffset = 0;
  mCurrentX = 0;
  mNextX = 0;
  mMaxX = Integer.MAX_VALUE;
  mScroller = new Scroller(getContext());
  mGesture = new GestureDetector(getContext(), mOnGesture);
 }

 @Override
 public void setOnItemSelectedListener(
   AdapterView.OnItemSelectedListener listener) {
  mOnItemSelected = listener;
 }

 @Override
 public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
  mOnItemClicked = listener;
 }

 @Override
 public void setOnItemLongClickListener(
   AdapterView.OnItemLongClickListener listener) {
  mOnItemLongClicked = listener;
 }

 private DataSetObserver mDataObserver = new DataSetObserver() {

  @Override
  public void onChanged() {
   synchronized (HorizontalView.this) {
    mDataChanged = true;
   }
   invalidate();
   requestLayout();
  }

  @Override
  public void onInvalidated() {
   reset();
   invalidate();
   requestLayout();
  }

 };

 @Override
 public ListAdapter getAdapter() {
  return mAdapter;
 }

 @Override
 public View getSelectedView() {
  // TODO: implement
  return null;
 }

 @Override
 public void setAdapter(ListAdapter adapter) {
  if (mAdapter != null) {
   mAdapter.unregisterDataSetObserver(mDataObserver);
  }
  mAdapter = adapter;
  mAdapter.registerDataSetObserver(mDataObserver);
  reset();
 }

 private synchronized void reset() {
  initView();
  removeAllViewsInLayout();
  requestLayout();
 }

 @Override
 public void setSelection(int position) {
  // TODO: implement
 }

 private void addAndMeasureChild(final View child, int viewPos) {
  LayoutParams params = child.getLayoutParams();
  if (params == null) {
   params = new LayoutParams(LayoutParams.FILL_PARENT,
     LayoutParams.FILL_PARENT);
  }

  addViewInLayout(child, viewPos, params, true);
  child.measure(
    MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
    MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
 }

 @Override
 protected synchronized void onLayout(boolean changed, int left, int top,
   int right, int bottom) {
  super.onLayout(changed, left, top, right, bottom);

  if (mAdapter == null) {
   return;
  }

  if (mDataChanged) {
   int oldCurrentX = mCurrentX;
   initView();
   removeAllViewsInLayout();
   mNextX = oldCurrentX;
   mDataChanged = false;
  }

  if (mScroller.computeScrollOffset()) {
   int scrollx = mScroller.getCurrX();
   mNextX = scrollx;
  }

  if (mNextX <= 0) {
   mNextX = 0;
   mScroller.forceFinished(true);
  }
  if (mNextX >= mMaxX) {
   mNextX = mMaxX;
   mScroller.forceFinished(true);
  }

  int dx = mCurrentX - mNextX;

  // removeNonVisibleItems(dx);
  fillList(dx);
  positionItems(dx);

  mCurrentX = mNextX;

  if (!mScroller.isFinished()) {
   post(new Runnable() {
    @Override
    public void run() {
     requestLayout();
    }
   });

  }
 }

 private void fillList(final int dx) {
  int edge = 0;
  View child = getChildAt(getChildCount() - 1);
  if (child != null) {
   edge = child.getRight();
  }
  fillListRight(edge, dx);

  edge = 0;
  child = getChildAt(0);
  if (child != null) {
   edge = child.getLeft();
  }
  fillListLeft(edge, dx);

 }

 private void fillListRight(int rightEdge, final int dx) {
  while (rightEdge + dx < getWidth()
    && mRightViewIndex < mAdapter.getCount()) {

   View child = mAdapter.getView(mRightViewIndex,
     mRemovedViewQueue.poll(), this);
   addAndMeasureChild(child, -1);
   rightEdge += child.getMeasuredWidth();

   if (mRightViewIndex == mAdapter.getCount() - 1) {
    mMaxX = mCurrentX + rightEdge - getWidth();
   }

   if (mMaxX < 0) {
    mMaxX = 0;
   }
   mRightViewIndex++;
  }

 }

 private void fillListLeft(int leftEdge, final int dx) {
  while (leftEdge + dx > 0 && mLeftViewIndex >= 0) {
   View child = mAdapter.getView(mLeftViewIndex,
     mRemovedViewQueue.poll(), this);
   addAndMeasureChild(child, 0);
   leftEdge -= child.getMeasuredWidth();
   mLeftViewIndex--;
   mDisplayOffset -= child.getMeasuredWidth();
  }
 }

 /*
  * private void removeNonVisibleItems(final int dx) { View child =
  * getChildAt(0); while(child != null && child.getRight() + dx <= 0) {
  * mDisplayOffset += child.getMeasuredWidth();
  * mRemovedViewQueue.offer(child); removeViewInLayout(child);
  * mLeftViewIndex++; child = getChildAt(0);
  * 
  * }
  * 
  * child = getChildAt(getChildCount()-1); while(child != null &&
  * child.getLeft() + dx >= getWidth()) { mRemovedViewQueue.offer(child);
  * removeViewInLayout(child); mRightViewIndex--; child =
  * getChildAt(getChildCount()-1); } }
  */

 private void positionItems(final int dx) {
  if (getChildCount() > 0) {
   mDisplayOffset += dx;
   int left = mDisplayOffset;
   for (int i = 0; i < getChildCount(); i++) {
    View child = getChildAt(i);
    int childWidth = child.getMeasuredWidth();
    child.layout(left, 0, left + childWidth,
      child.getMeasuredHeight());
    left += childWidth;
   }
  }
 }

 public synchronized void scrollTo(int x) {
  mScroller.startScroll(mNextX, 0, x - mNextX, 0);
  requestLayout();
 }

 @Override
 public boolean dispatchTouchEvent(MotionEvent ev) {
  boolean handled = super.dispatchTouchEvent(ev);
  handled |= mGesture.onTouchEvent(ev);
  return handled;
 }

 protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
  synchronized (HorizontalView.this) {
   mScroller.fling(mNextX, 0, (int) -velocityX, 0, 0, mMaxX, 0, 0);
  }
  requestLayout();

  return true;
 }

 protected boolean onDown(MotionEvent e) {
  mScroller.forceFinished(true);
  return true;
 }

 private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {

  @Override
  public boolean onDown(MotionEvent e) {
   return HorizontalView.this.onDown(e);
  }

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   return HorizontalView.this.onFling(e1, e2, velocityX, velocityY);
  }

  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2,
    float distanceX, float distanceY) {

   synchronized (HorizontalView.this) {
    mNextX += (int) distanceX;
   }
   requestLayout();

   return true;
  }

  @Override
  public boolean onSingleTapConfirmed(MotionEvent e) {
   for (int i = 0; i < getChildCount(); i++) {
    View child = getChildAt(i);
    if (isEventWithinView(e, child)) {
     if (mOnItemClicked != null) {
      mOnItemClicked.onItemClick(HorizontalView.this, child,
        mLeftViewIndex + 1 + i,
        mAdapter.getItemId(mLeftViewIndex + 1 + i));
     }
     if (mOnItemSelected != null) {
      mOnItemSelected.onItemSelected(HorizontalView.this,
        child, mLeftViewIndex + 1 + i,
        mAdapter.getItemId(mLeftViewIndex + 1 + i));
     }
     break;
    }

   }
   return true;
  }

  @Override
  public void onLongPress(MotionEvent e) {
   int childCount = getChildCount();
   for (int i = 0; i < childCount; i++) {
    View child = getChildAt(i);
    if (isEventWithinView(e, child)) {
     if (mOnItemLongClicked != null) {
      mOnItemLongClicked.onItemLongClick(HorizontalView.this,
        child, mLeftViewIndex + 1 + i,
        mAdapter.getItemId(mLeftViewIndex + 1 + i));
     }
     break;
    }

   }
  }

  private boolean isEventWithinView(MotionEvent e, View child) {
   Rect viewRect = new Rect();
   int[] childPosition = new int[2];
   child.getLocationOnScreen(childPosition);
   int left = childPosition[0];
   int right = left + child.getWidth();
   int top = childPosition[1];
   int bottom = top + child.getHeight();
   viewRect.set(left, top, right, bottom);
   return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
  }
 };

}




4. Now my Activity class(HorizontalListView.java ) and adapter class
   (HorizontalImageAdapter.java )




A.HorizontalListView.java


package com.mukesh.horizontallistview;

import java.util.ArrayList;
import java.util.List;

import com.mukesh.ui.HorizontalView;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;

public class HorizontalListView extends Activity {

 private List drawables;
 private HorizontalImageAdapter imageAdapter;
 private HorizontalView listView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_horizontal_list_view);

  listView = (HorizontalView) findViewById(R.id.gallery);
  getDrawablesList();
  setupUI();
 }

 private void setupUI() {
  imageAdapter = new HorizontalImageAdapter(this, drawables);

  listView.setAdapter(imageAdapter);

 }

 private void getDrawablesList() {

      drawables = new ArrayList();
      drawables.add(getResources().getDrawable(R.drawable.natureimage1));
      drawables.add(getResources().getDrawable(R.drawable.natureimage2));
      drawables.add(getResources().getDrawable(R.drawable.natureimage3));
      drawables.add(getResources().getDrawable(R.drawable.natureimage4));
      drawables.add(getResources().getDrawable(R.drawable.natureimage5));
      drawables.add(getResources().getDrawable(R.drawable.natureimage6));
      drawables.add(getResources().getDrawable(R.drawable.natureimage7));
      drawables.add(getResources().getDrawable(R.drawable.natureimage8));
      drawables.add(getResources().getDrawable(R.drawable.natureimage9));
      drawables.add(getResources().getDrawable(R.drawable.natureimage10));
      drawables.add(getResources().getDrawable(R.drawable.natureimage12));
      drawables.add(getResources().getDrawable(R.drawable.natureimage13));
      drawables.add(getResources().getDrawable(R.drawable.natureimage15));

 }

}




B. HorizontalImageAdapter.java 
package com.mukesh.horizontallistview; import java.util.List; import android.app.Activity; import android.graphics.drawable.Drawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; public class HorizontalImageAdapter extends BaseAdapter { private Activity context; private static ImageView imageView; private List plotsImages; private static ViewHolder holder; private LayoutInflater l_Inflater; public HorizontalImageAdapter(Activity context, List plotsImages) { this.context = context; this.plotsImages = plotsImages; l_Inflater = LayoutInflater.from(context); } @Override public int getCount() { return plotsImages.size(); } @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) { if (convertView == null) { holder = new ViewHolder(); convertView = l_Inflater.inflate(R.layout.listview_item, null); holder = new ViewHolder(); holder.imageView = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.imageView.setImageDrawable(plotsImages.get(position)); return convertView; } private static class ViewHolder { ImageView imageView; } }


This is what I am doing to show Horizontal listviw in android.


Cheers :)
Enjoy Coding....

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

72 comments:

  1. I already shared all my code in above blog...you just need to copy and paste those code in your app.

    ReplyDelete
  2. hi mukesh thanks for this view

    Can you help me ?
    how to create listview like samsung mobile contacts
    item when swiping left or right call,and sms action
    performed with animation respectively.

    Please if you can do help me out

    Thanks in advanced

    ReplyDelete
  3. Sure, First,I have to check the view on samsung b'coz your requirement is not clear to me....or will explain something more , what exactly you want.

    ReplyDelete
  4. Why your your holder class as static?

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. A ViewHolder class is a static inner class in your adapter which hold references to the relevant Views in your layout. This reference is assigned to the View which represent the row layout as a tag via the setTag() method.

    Use static only if your ViewHolder is an inner class. for better performance, Also If your ViewHolder class will not be static - you have to provide instance of parent class.

    For more you can refer Static nested class in Java.check this link
    http://stackoverflow.com/questions/253492/static-nested-class-in-java-why

    ReplyDelete
  7. Is it possible to show a scrollbar below itens?

    ReplyDelete
  8. Hello pachecaum,
    I am not getting your question ?? what exactly you want.....

    ReplyDelete
  9. why setOnScrollListener is not working for horizontal list view?
    any idea?

    ReplyDelete
  10. Thank you!!! good to use!!

    ReplyDelete
  11. hi mukesh is it possible to sqlite data shows in horizontal scroll view

    ReplyDelete
  12. Hey Mukesh,

    thanks a lot for sharing your work!

    I am trying to set up an infinite (circular) HorizontalListView. So I'd like to set the count of my adapter to Integer.MAX_VALUE and then placing the list in the middle. Therefore using scrollTo() wouldn't be a good idea.

    However, I figured we need to implement setSelection()... Do you have any Idea how this can be done?

    ReplyDelete
  13. Hello ,
    for scrolling to some position , scrollTo(pos) will work.
    But, In your case for making it circular I think you have take two variable max and min and very time you have to check if you are at max position, then bind the listview, but for this you have to make two array:
    first array:[min,0,1,2,3.......,9,max]
    second array:[max,min,0,1,2........9]
    ----
    something like this.....I didn't implement this so I am not sure.

    ReplyDelete
  14. hiii Mukesh i use your code but it is not running properly. it is giving error :

    android.view.InflateException: Binary XML file line #25: Error inflating class com.mukesh.ui.HorizontalView

    can you please tell me what am i doing wrong here..

    if u give ur Id than i can mail my code to You.

    ReplyDelete
  15. Hey hi,
    please provide me some more log or you can mail me your code on my id- mukesh421985@gmail.com

    ReplyDelete
  16. hello Mukesh,

    I have succefully implemented HorizontalImageView in my application. Thanks a tons.

    But i am facing problem in moving view to a specific position while populating.

    I want to show deafult position of scroll list at mid. So user can scroll left and right.

    i have tried to change initView() mRightViewIndex to 6 or else. It start showing list from number 6. 0-5 images simply discarded.

    please provide solution so i can make centered HorizontalList with left right scrolling.

    ReplyDelete
  17. How can we set the edge glow effect at the end of this list ?

    ReplyDelete
  18. Hello ciddhi
    we can do this by adding border..

    ReplyDelete
  19. Can u pls post an easy, horizontal list view tutorial. U kno, just using basic text?? Pls :)

    ReplyDelete
  20. thks !!!! but srollTo(position) and setSelection(position) don't work

    ReplyDelete
  21. Hello Lagfeb,
    I think you are calling position=1 or 2....
    In this sample the scrollTo() scroll at some distance so use scrollTo(50) (50dp).
    Yes, like vertical scroll there is no setSelection
    method but you can apply the selevtion when you are creating the adapter view.

    ReplyDelete
  22. Hi very useful post thanks a lot. Nice work.

    ReplyDelete
  23. Hi Mukesh, very useful post. Thanks a lot. Nice work.

    ReplyDelete
  24. This comment has been removed by the author.

    ReplyDelete
  25. hey mukesh this is nice tutorial :)
    i try your code.. then i click item and not change why?

    ReplyDelete
  26. Hello Danax,
    Check your adapter code logic....
    holder.imageView.setImageDrawable(plotsImages.get(position));

    You are doing something wrong, cross check the adapter logic

    ReplyDelete
  27. code not working plz give me complete code on my email address plzz smartguy12000@gmail.com

    ReplyDelete
  28. i need this app complete source code is not working please mail me smartguy12000@gmail.com

    ReplyDelete
  29. Hello Farhan, will you please provide me log error which you are facing. I have added all the code in my blog, this is complete working source
    code.If you still facing any error then you can feel free to share your code with me on my gmail id : mukesh421985@gmail.com

    ReplyDelete
  30. Works great! Thanks for sharing.

    ReplyDelete
  31. i want to add text also below each image
    how can i do this

    ReplyDelete
  32. Hello Shaimaa,
    Add a textview in listview_item.xml and then bind it with the adapter.

    ReplyDelete
  33. hello mukesh,

    I just noticed a bug in HorizontalListView, which forces the item view in list to be of the same height and width as of the list this is due to a small bug in your code in HorizontalListView's addAndMeasureChild() method

    at the end of this method you measured the child using ::>
    MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
    MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));


    the getWidth() and getHeight() returns the width and Height of listviews not the Item view

    so please changed it to this ::>

    child.measure(MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.AT_MOST),
    MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.AT_MOST));

    this will solve your problem :)


    With regards
    Gagandeep Singh

    ReplyDelete
  34. Hello Mukesh,

    Thanks for your great tutorial.I need to display 1 item visible at a time.when user scrolls it will display the another item.please find me the solution.

    Thanks & Regards,
    Venkatesan.R

    ReplyDelete
  35. Hello venkatsys,

    When u are binding the list adapter just try to set the visibility true on the basis of item position you want to display.

    ReplyDelete
  36. Hi, Is it possible to set a custom offset into xml between items or a divider like in listview?

    Thanks
    Fabrizio

    ReplyDelete
  37. Hi,
    great job!
    Is it possible to show arrows (like expandablelistview) when the horizontalview is scrollable?

    Thanks a lot
    Fabrizio

    ReplyDelete
  38. thanks i need to select and focus specific item from horizontal listview. for this i need to use setSelection () method. can u help how to define it

    ReplyDelete
  39. On running above code i am getting error like this:

    02-28 08:42:42.117: E/AndroidRuntime(3309): FATAL EXCEPTION: main
    02-28 08:42:42.117: E/AndroidRuntime(3309): Process: com.example.bbcdemo, PID: 3309
    02-28 08:42:42.117: E/AndroidRuntime(3309): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bbcdemo/com.example.bbcdemo.HorizontalView}: java.lang.InstantiationException: can't instantiate class com.example.bbcdemo.HorizontalView
    02-28 08:42:42.117: E/AndroidRuntime(3309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
    02-28 08:42:42.117: E/AndroidRuntime(3309): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
    02-28 08:42:42.117: E/AndroidRuntime(3309): at android.app.ActivityThread.access$700(ActivityThread.java:135)
    02-28 08:42:42.117: E/AndroidRuntime(3309): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
    02-28 08:42:42.117: E/AndroidRuntime(3309): at android.os.Handler.dispatchMessage(Handler.java:102)
    .....................................

    ReplyDelete
  40. Hello,

    How can I scroll multiple HorizontallScroll View at the same time, the lists haven't the same width.

    Thanks in advance.

    ReplyDelete
  41. Hi! Why OnItemClickListener is not working for HorizontalListView?((

    ReplyDelete
  42. How we can do horizontal scrollview pagination like Pulse app

    ReplyDelete
  43. Hello,
    This works really good. But do u have code to make the horizantal listview as curved towards the base?

    ReplyDelete
  44. Hello Megha,
    I don't have the code but we can manage our view like

    ReplyDelete
  45. Hi,
    Can i expect any help in making horizantal listview as curved towards the base ?

    ReplyDelete
  46. I have used Horizantal list view. And instead of inflating any layout in BaseAdapter, I have created a custom view. my code is given below:

    Import android.content.Context;
    import android.graphics.Canvas;
    import android.util.DisplayMetrics;
    import android.widget.ImageView;

    public class MyView extends ImageView {

    private static final int MAX_INDENT = 300;

    public MyView(Context context) {
    super(context);
    }

    public void onDraw(Canvas canvas){
    canvas.save();
    float indent = getIndent(getX());
    //Part of the magic happens here too
    canvas.translate(0,indent);
    super.onDraw(canvas);
    canvas.restore();
    }

    public float getIndent(float distance){
    float y_vertex = MAX_INDENT;
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    float x_vertex = displayMetrics.heightPixels / 2 / displayMetrics.density;
    double a = ( 0 - x_vertex ) / ( Math.pow(( 0 - y_vertex), 2) ) ;
    float indent = (float) (a * Math.pow((distance - y_vertex), 2) + x_vertex);
    return indent;
    }
    }

    But its of no use.

    ReplyDelete
  47. Hey,
    Can i expect any code help from you? Its urgent,I am in mid of my coding. Do reply

    ReplyDelete
    Replies
    1. Provide me the ui, how you want on screen, and if possible then share code where you are trying to implement.

      Delete
  48. hi i used ur code but i cannot add space between each items in listview wat to do for that?

    ReplyDelete
  49. Hi Mukesh,
    your code was very useful for me. but i want to implement an horizontal list in which when i tap on an item, it should return the name of the item i tap. but by using your code its always displaying the name of the first item. It would be really thankful if you can help me.

    ReplyDelete
  50. Hello mukesh sir,
    your tutorials are very good and i need a help from you that"how to control web view video by using seek bar(i mean video forward and backward) and is it possible if it is possible can you explain me or send to mail"

    ReplyDelete
  51. Hi Mukesh ,
    I am getting error "The type HorizontalView must implement the inherited abstract method AdapterView.setAdapter(Adapter)" and its saying to add unimplented method.

    ReplyDelete
  52. Hi,

    Can i get the code for getting the preferences from one or more activities and place those preferences in single activity. this is confusing for me. plz help me.

    thank you.

    ReplyDelete
  53. thank for all. how i can custom view so good same you. pl show me step by step i need to ?

    ReplyDelete
  54. Kuş Vuralım İstersen26 June 2014 at 16:20

    you're hair so cute, from turkey

    ReplyDelete
  55. Hey Mukesh,

    thanks a lot for sharing your work, its been a great help!

    however i tried to bind two textviews to adapter , but they are nor displayed on screen , can you please share code to bind them properly..!!

    please please please help..!!

    ReplyDelete
  56. viewHolder.imageView.setImageDrawable( imageViewList.get(position)); I am getting error here saying that "setImageDrawable(android.graphics.drawable.Drawable) in Imageview cannot be applied to (java.lang.Object). I don't know what is the problem here.

    ReplyDelete
  57. hey mukesh,
    i want selected list item. means when i click on list item then change color of that item?
    can you help me?

    ReplyDelete
  58. how to set spacing between two items in horizontal listview ?

    ReplyDelete
  59. Hi
    how i can load more when scroll to the right screen?
    thank you

    ReplyDelete
  60. hai yadav,
    i need show 2 items in one screen.how to i show it?.pls help as soon as possible

    ReplyDelete
  61. hello,
    I have done your tutorial and its run successfully
    but I'm facing some problem here.
    OnClickListener ,or OnItemClickListener not working as worked in simple listview
    plz help

    ReplyDelete
  62. Can you please provide the implementation for setSelection() method...its urgent

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.