Wednesday, 31 July 2013

Swipe in android

Swipe or OnFling Event Android

This is a simple application Demonstrating Swipe or onFling() event on ListView.

Swipe views provide lateral navigation between sibling screens such as tabs with a horizontal finger gesture (a pattern sometimes known as horizontal paging). This lesson teaches you how to create a tab layout with swipe views for switching between tabs, or how to show a title strip instead of tabs.
Swipe View Design
Before implementing these features, you should understand the concepts and recommendations as described inDesigning Effective Navigation and the Swipe Views design guide.



1.Following is the MainActivity.java of the application



import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class DemoSwipe extends Activity {
 ListView lvCountry;
 String[] country = { "India", "USA", "Russsia", "China", "Pakistan",
   "Canada", "UK" };
 SwipeGestureListener gestureListener;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  lvCountry = (ListView) findViewById(R.id.lv_country);
  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
    DemoSwipe.this, android.R.layout.simple_list_item_1, country);
  lvCountry.setAdapter(arrayAdapter);
  gestureListener = new SwipeGestureListener(DemoSwipe.this);
  lvCountry.setOnTouchListener(gestureListener);

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 class SwipeGestureListener extends SimpleOnGestureListener implements
   OnTouchListener {
  Context context;
  GestureDetector gDetector;

//set min & max distance for swipe.

  static final int SWIPE_MIN_DISTANCE = 120;
  static final int SWIPE_MAX_OFF_PATH = 250;
  static final int SWIPE_THRESHOLD_VELOCITY = 200;

  public SwipeGestureListener() {
   super();
  }

  public SwipeGestureListener(Context context) {
   this(context, null);
  }

  public SwipeGestureListener(Context context, GestureDetector gDetector) {

   if (gDetector == null)
    gDetector = new GestureDetector(context, this);

   this.context = context;
   this.gDetector = gDetector;
  }

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {

   final int position = lvCountry.pointToPosition(
     Math.round(e1.getX()), Math.round(e1.getY()));

   String countryName = (String) lvCountry.getItemAtPosition(position);

   if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
    if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH
      || Math.abs(velocityY) < SWIPE_THRESHOLD_VELOCITY) {
     return false;
    }
    if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
     Toast.makeText(DemoSwipe.this, "bottomToTop" + countryName,
       Toast.LENGTH_SHORT).show();
    } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
     Toast.makeText(DemoSwipe.this,
       "topToBottom  " + countryName, Toast.LENGTH_SHORT)
       .show();
    }
   } else {
    if (Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY) {
     return false;
    }
    if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
     Toast.makeText(DemoSwipe.this,
       "swipe RightToLeft " + countryName, 5000).show();
    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
     Toast.makeText(DemoSwipe.this,
       "swipe LeftToright  " + countryName, 5000).show();
    }
   }

   return super.onFling(e1, e2, velocityX, velocityY);

  }

  @Override
  public boolean onTouch(View v, MotionEvent event) {

   return gDetector.onTouchEvent(event);
  }

  public GestureDetector getDetector() {
   return gDetector;
  }

 }
}


Screenshot:

1 comment:

Unknown said...

Best work done guys, creative contents are here.

Android App Developer in Pakistan