Step 1: Just extends below class and implement getListview method.

Step 2: The below MainActivity demonstrates in which direction single item should be swiped and gives the item position of clicked item. We have to manage it in adapter to swipe-delete operation from our own scratch.

SwipeListViewActivity.java:

[sourcecode language=”java” padlinenumbers=”2″]
public abstract class SwipeListViewActivity extends Activity {

private ListView list;
private int REL_SWIPE_MIN_DISTANCE;
private int REL_SWIPE_MAX_OFF_PATH;
private int REL_SWIPE_THRESHOLD_VELOCITY;

/**
*
* @return ListView
*/
public abstract ListView getListView();

/**
*
* @param isRight
* Swiping direction
* @param position
* which item position is swiped
*/
public abstract void getSwipeItem(boolean isRight, int position);

/**
* For single tap/Click
*
* @param adapter
* @param position
*/
public abstract void onItemClickListener(ListAdapter adapter, int position);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

DisplayMetrics dm = getResources().getDisplayMetrics();
REL_SWIPE_MIN_DISTANCE = (int) (120.0f * dm.densityDpi / 160.0f + 0.5);
REL_SWIPE_MAX_OFF_PATH = (int) (250.0f * dm.densityDpi / 160.0f + 0.5);
REL_SWIPE_THRESHOLD_VELOCITY = (int) (200.0f * dm.densityDpi / 160.0f + 0.5);
}

@Override
protected void onResume() {
super.onResume();
list = getListView();
if (list == null) {
new Throwable("Listview not set exception");
}

@SuppressWarnings("deprecation")
final GestureDetector gestureDetector = new GestureDetector(
new MyGestureDetector());

View.OnTouchListener gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
list.setOnTouchListener(gestureListener);

}

private void myOnItemClick(int position) {
if (position < 0)
return;
onItemClickListener(list.getAdapter(), position);

}

class MyGestureDetector extends SimpleOnGestureListener {

private int temp_position = -1;

// Detect a single-click and call my own handler.
@Override
public boolean onSingleTapUp(MotionEvent e) {

int pos = list.pointToPosition((int) e.getX(), (int) e.getY());
myOnItemClick(pos);
return true;
}

@Override
public boolean onDown(MotionEvent e) {

temp_position = list
.pointToPosition((int) e.getX(), (int) e.getY());
return super.onDown(e);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (Math.abs(e1.getY() – e2.getY()) > REL_SWIPE_MAX_OFF_PATH)
return false;
if (e1.getX() – e2.getX() > REL_SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {

int pos = list
.pointToPosition((int) e1.getX(), (int) e2.getY());

if (pos >= 0 && temp_position == pos)
getSwipeItem(false, pos);
} else if (e2.getX() – e1.getX() > REL_SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {

int pos = list
.pointToPosition((int) e1.getX(), (int) e2.getY());
if (pos >= 0 && temp_position == pos)
getSwipeItem(true, pos);

}
return false;
}

}

}
<h3>[/sourcecode]

MainActivity.java:

[sourcecode language=”java” padlinenumbers=”2″]
public class MainActivity extends SwipeListViewActivity {

private ListView mListView;
private ArrayAdapter<String> mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.listView1);
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, new String[] { "Item 1",
"Item 2", "Item 2", "Item 3", "Item 4", "Item 5" });
mListView.setAdapter(mAdapter);
}

@Override
public ListView getListView() {
return mListView;
}

@Override
public void getSwipeItem(boolean isRight, int position) {
Toast.makeText(this,
"Swipe to " + (isRight ? "right" : "left") + " direction",
Toast.LENGTH_SHORT).show();
}

@Override
public void onItemClickListener(ListAdapter adapter, int position) {
Toast.makeText(this, "Single tap on item position " + position,
Toast.LENGTH_SHORT).show();
}

}

[/sourcecode]

Get the source code from the following link .
Let you comment on posts and encourage me for nice posts in future…

How to implement swipe delete in android like iphone ?
Tagged on:                                         

19 thoughts on “How to implement swipe delete in android like iphone ?

  • March 19, 2013 at 6:01 pm
    Permalink

    absolutely brilliant!
    thanks.
    please post source code of total swipe-delete operation.
    your efforts are much appreciated.

    Reply
  • May 8, 2013 at 5:37 am
    Permalink

    Hi, Can you post source code of total swipe-delete operation. Thanks for sharing!

    Reply
  • July 25, 2013 at 10:48 am
    Permalink

    just wanting to know if the working example will be posted?

    Reply
    • August 5, 2013 at 12:23 pm
      Permalink

      I have updated in post with source code. Please check it once.

      Reply
  • August 2, 2013 at 11:50 am
    Permalink

    Really …….Superb n time saving post getting alot google ….Thnx a lot…

    Reply
  • November 9, 2013 at 5:37 pm
    Permalink

    Thanks a lot, it’s what i was looking for!

    Reply
  • December 4, 2013 at 11:39 am
    Permalink

    Is it Possible to get the source code of this project???
    Thanks in advance!!!!

    Reply
  • March 13, 2014 at 11:58 am
    Permalink

    Can anyone provide me source code ? I am not able to get from link provided..

    Reply
  • March 24, 2014 at 9:32 am
    Permalink

    Anyone who can tell me how to implement this on my Note 3? Im not a programmer, just a tech enthusiast and sorely miss my swipe delete function of iphone after migrating to android Note 3 last week.

    Reply
  • June 2, 2014 at 2:29 pm
    Permalink

    Anyone???? Please help a newbie enthusiast.

    Reply
  • January 19, 2015 at 12:51 pm
    Permalink

    please send me the full source code

    Reply
    • February 28, 2015 at 6:18 am
      Permalink

      Hi Arihant,

      Thanks for visiting my blog, I have updated the source code which is seems to be broken earlier.

      Reply
  • May 30, 2016 at 7:20 am
    Permalink

    i think this code is not working i want to swipe right to left then icon to show(delete icon) when i click to delete perform delete otherwise swipeing is close

    Reply
  • June 27, 2016 at 2:18 pm
    Permalink

    Thanks, It’s completely work to me !

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *