Friday, June 21, 2013

Detecting Swipe Event In Android

To detect Touch Event and other events like Left to Right sweep and Right to Left sweep, we use  MotionEvent  class.

MotionEvent


MotionEvent object is used to report movement (mouse, pen, finger, trackball) events. Motion events may hold either absolute or relative movements and other data, depending on the type of device.

Motion events describe movements in terms of an action code and a set of axis values. The action code specifies the state change that occurred such as a pointer going down or up. The axis values describe the position and other movement properties.

For example, when the user first touches the screen, the system delivers a touch event to the appropriate View with the action code ACTION_DOWN and a set of axis values that include the X and Y coordinates of the touch and information about the pressure, size and orientation of the contact area.


In the code below I have described with proper Source Code about detecting Swap Events but first we should understand the basics.

onTouchEvent () method ogets called when User performs any touch event on screen

when a User swaps from Left to Right or Right to left

user first touches on the screen ( lets say first x coordinate is x1) holds ,swaps  then leaves the screen  (lets say second x coordinate is x2)

so if x2> x1  it means Left to Right sweep has been performed and
     if x2<x1   it means Right to Left sweep has been performed

Similarly we can track UP to Down and Down to UP swap

if  y2> y1  it means UP to Down sweep has been performed and
if  y2<y1   it means Down to UP sweep has been performed

Example with Source Code


main.xml


TouchEvent



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

        <TextView
            android:layout_marginTop="80dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:gravity="center"
            android:textColor="#000099"
            android:textSize="30dp"
            android:text="Detect Swap Event Demo" />
       
   
</LinearLayout>




MainActivity.java


public class MainActivity extends Activity
{
            float x1,x2;
            float y1, y2;
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);
            }
          
            //
onTouchEvent () method gets called when User performs any touch event on screen 
           // Method to handle touch event like left to right swap and right to left swap
                        public boolean onTouchEvent(MotionEvent touchevent)
                        {
                                     switch (touchevent.getAction())
                                     {
                                            // when user first touches the screen we get x and y coordinate
                                             case MotionEvent.ACTION_DOWN:
                                             {
                                                 x1 = touchevent.getX();
                                                 y1 = touchevent.getY();
                                                 break;
                                            }
                                             case MotionEvent.ACTION_UP:
                                             {
                                                 x2 = touchevent.getX();
                                                 y2 = touchevent.getY(); 


                                                 / /if left to right
sweep event on screen
                                                 if (x1 < x2)
                                                 {
                                                     Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();
                                                  }
                                               
                                                 // if right to left sweep event on screen

                                                 if (x1 > x2)
                                                 {
                                                     Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
                                                 }
                                               
                                                 // if UP to Down
sweep event on screen
                                                 if (y1 < y2)
                                                 {
                                                     Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show();
                                                 }
                                               
                                                 / /if Down to UP
sweep event on screen
                                                 if (y1 > y2)
                                                 {
                                                     Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
                                                  }
                                                 break;
                                             }
                                     }
                                     return false;
                        }

  
}





TouchEvent




55 comments:

  1. thanks very simple and good example

    ReplyDelete
  2. Very Good Example Sir But How Do You Do It Using A Image i.e., Sliding a Image and Viewing A Text ??/

    ReplyDelete
  3. Good But How To Swipe With A Image To Top To Display Something ??? Please Help Out !!!

    ReplyDelete
  4. It's simple but sometimes left-right swap and down-up swap both performs, tell me why?

    ReplyDelete
    Replies
    1. If the user doesn't swipe exactly left to right (y1=y2) or up to down (x1=x2) then both events are detected. To solve this compare the difference of the x values with the difference of the y values. A greater difference between the x values means a left to right swipe. In the code add two float variables after the float y1,y2;
      float diffx, diffy;
      Right before the first if statement include the lines
      diffx = x2-x1;
      diffy = y2-y1;
      Within the if statements add as follows:
      if (x1 < x2 && Math.abs(diffy) < Math.abs(diffx))
      if (x2 < x1 && Math.abs(diffy) < Math.abs(diffx))
      if (y1 < y2 && Math.abs(diffx) < Math.abs(diffy))
      if (y2 < y1 && Math.abs(diffy) < Math.abs(diffx))

      Delete
    2. Hi sir how exactly are we going to add the if statements? could you please provide a sample. thank you

      Delete
    3. down to up dosen't work....when i include above code... and left to right n down to up both work on same time...give a solution..thanks in advance

      Delete
  5. Very clear tutorial, I want to ask though.. here we check up and down left and right if we want diagonal we just combine the up and left for example?

    ReplyDelete
  6. Very good example, but if we want to check diagonal? we just combine for example the up and left at the same time?

    ReplyDelete
  7. that's very good example but tost is running which not required at one action also perform downup and updaown in this example.........

    ReplyDelete
  8. Great tutorial with clear explanation.

    ReplyDelete
  9. Simple is best, Thank you.

    ReplyDelete
  10. how can we get on click () including this all...

    ReplyDelete
  11. i want to handle onclick on listitem too,among these all events... how can i do that

    ReplyDelete
  12. Thank you, very simple, very clear.

    ReplyDelete
  13. Thank you, very simple, very clear.

    ReplyDelete
  14. very simple and clear.. thanks alot and keep it up

    ReplyDelete
  15. good example... bt what if we wnt to swipe to another page? how can we do this?

    ReplyDelete
  16. If i want to just swipe horiztonally, how do I accomplish this? I tried to disable "y" but it still allows swipe vertically.

    ReplyDelete
  17. Thanks ! That's great.

    ReplyDelete
  18. How can i enable scrolling Y axis with this code?

    ReplyDelete
  19. Hello,
    if (x1 > x2)
    {
    Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
    How to change TextView
    }

    ReplyDelete
    Replies
    1. First give id to TextView of Detect Swap Event Demo in main.xml
      and then
      if (x1 > x2)
      {
      Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
      // to change TextView
      TextView tv = (TextView) findViewById(R.id.textView1);
      tv.setText("It has now Moved to Right");
      }

      Delete
    2. Glad to see fuuastians reply :) (Y)

      Delete
  20. Very Helpful for beginners!!

    ReplyDelete
  21. nice bro!
    its work fine here.

    thank you!

    ReplyDelete
  22. very nice sir,thank you so much!!!

    ReplyDelete
  23. Thanks it help us very much

    ReplyDelete
  24. Thaks A LOT. It REALLY helps me.

    ReplyDelete
  25. 👏👏👆👍👍

    ReplyDelete
  26. thaaaaaanks alooooot
    i searched for this method about one weak but i didnt find any thing
    thank you very much

    ReplyDelete
  27. Really nice,clear explanation got..,Thanks

    ReplyDelete
  28. Really nice,clear explanation,simple code,thanks

    ReplyDelete
  29. Hello, your article is very nice...
    if(x1>x2)
    {
    How to highlight textview part from x1 to x2?
    }

    ReplyDelete
  30. hello .. in above snippet , if i want to highlight that textview contain on which swapping has been performed , so how can i do that?

    ReplyDelete
  31. however you will need them reeling or on one leg. On the off chance that they move back after a phony assault, this may be sufficient to get them cockeyed and afterward you can rearrange bounce advance and apply the scope. broom and dustpan

    ReplyDelete
  32. Read Shayari in Hindi, Best & New Hindi Shayari on Love, Sad, Funny, Friendship, Bewafai, Dard, GoodMorning, GoodNight, Judai, Mehakal, Whatsapp Status in Hindi & English

    Shayari By Categories:-

    Ab Shayari.Guru
    MAHAKAL STATUS
    MOTIVATIONAL QUOTES
    ATTITUDE STATUS
    BEWAFA SHAYARI
    SHARABI SHAYARI

    ReplyDelete
  33. Pls rewrite to Kotlin

    ReplyDelete
  34. Awesome and a great value article. Really nice really good one



    lovely ki shayari

    ReplyDelete
  35. Hello! I love your writing very much! How can we stay in touch with regards to your posts on AOL? I need an expert to solve my issue. Perhaps you're the one! I am looking forward to seeing you. Horoscope Reading Specialist , Husband & Wife Problems Specialist,

    ReplyDelete
  36. I got what you intend, thankyou for putting up.Woh I am thankful to find this website through google. “Food is the most primitive form of comfort. Get Your Love Back In Melbourne, Famous Astrologer In Melbourne Things To Know Before You Buy, |
    The Single Best Strategy To Use For Top Astrologer In Sydney,

    ReplyDelete
  37. āđƒāļ™āļ›ัāļˆāļˆุāļšัāļ™āļ—ี่āļ—ุāļ„āļ™āļ•่āļēāļ‡āļ็āļĄāļ­āļ‡āļŦāļēāļŠิ่āļ‡āļ—ี่āļˆāļ°āļ­āļģāļ™āļ§āļĒāļ„āļ§āļēāļĄāļŠāļ°āļ”āļ§āļāđƒāļŦ้āļัāļšāļ•ัāļ§āđ€āļ­āļ‡ āđ€āļžāļĢāļēāļ°āđ€āļ—āļ„āđ‚āļ™āđ‚āļĨāļĒีāļ•่āļēāļ‡āđ† āļ—ี่āđ€āļ‚้āļēāļĄāļēāļĄีāļŠ่āļ§āļ™āļŠāļģāļ„ัāļāđƒāļ™āļŠีāļ§ิāļ• āđ‚āļ”āļĒāđ€āļ‰āļžāļēāļ°āđƒāļ™āđ€āļĢื่āļ­āļ‡āļ‚āļ­āļ‡ āđāļ—āļ‡āļšāļ­āļĨ āļ‹ึ่āļ‡āļˆāļ°āļ”ีāļāļ§่āļēāđ„āļŦāļĄ āļŦāļēāļāļ—่āļēāļ™āđ„āļ”้āđ€āļ”ิāļĄāļžัāļ™āļ­āļĒู่āļ—ี่āļš้āļēāļ™ āļœ่āļēāļ™āļ—āļēāļ‡āđ‚āļ—āļĢāļĻัāļžāļ—์āļĄืāļ­āļ–ืāļ­ āđ„āļ”้āļ•āļĨāļ­āļ” 24 āļŠั่āļ§āđ‚āļĄāļ‡ āļัāļšāđ€āļ§็āļšāļžāļ™ัāļ™ UFA888 āđ€āļ›็āļ™āđ€āļ§็āļšāļžāļ™ัāļ™āļšāļ­āļĨāļ­āļ­āļ™āđ„āļĨāļ™์āļŠāļēāļĄāļēāļĢāļ–āļ•āļ­āļšāđ‚āļˆāļ—āļĒ์āļ•่āļ­āļ„āļ§āļēāļĄāļ•้āļ­āļ‡āļāļēāļĢāļ‚āļ­āļ‡āļœู้āļ„āļ™ āđāļĨāļ°āļ—āļģāđƒāļŦ้āđ„āļ”้āļĢัāļšāļ„āļ§āļēāļĄāļŠāļ°āļ”āļ§āļāļŠāļšāļēāļĒāļĄāļēāļāļāļ§่āļēāđƒāļ™āļŦāļĨāļēāļĒ āđ† āļ”้āļēāļ™ āļ­ีāļāļ—ั้āļ‡āļĒัāļ‡āļŠāļēāļĄāļēāļĢāļ–āļŠāļĢ้āļēāļ‡āļĢāļēāļĒāđ„āļ”้ āđ„āļ”้āļ”ีāļāļ§่āļēāļāļēāļĢāđ€āļ”ิāļĄāļžัāļ™āđāļšāļšāđ€āļ”ิāļĄ āđ† āđ‚āļ”āļĒāđ€āļ‰āļžāļēāļ°āļ—ี่āđ€āļ§็āļšāļ‚āļ­āļ‡āđ€āļĢāļē āļ—ี่āļĄีāļ­ัāļ•āļĢāļēāļāļēāļĢāļˆ่āļēāļĒāļœāļĨāļāļģāđ„āļĢāļ—ี่āļĄāļēāļāļāļ§่āļēāļ•āļēāļĄāđ‚āļ•๊āļ°āļšāļ­āļĨ āļŦāļĢืāļ­āđ€āļ§็āļšāļ­ื่āļ™ āđ† āļ–ึāļ‡ 3 āđ€āļ—่āļēāđ€āļĨāļĒāļ—ีāđ€āļ”ีāļĒāļ§

    ReplyDelete
  38. āļŦāļēāļāļžูāļ”āļ–ึāļ‡āđ€āļ§็āļšāđ„āļ‹āļ•์āļ—ี่āđ€āļŦāļĄāļēāļ°āđāļ่āļāļēāļĢāđ€āļ”ิāļĄāļžัāļ™ āļšāļ­āļĨāđ‚āļĨāļ 2022 āļĄāļēāļāļ—ี่āļŠุāļ” āļŠื่āļ­āļ‚āļ­āļ‡āđ€āļ§็āļš UFA888 āļ็āļ„āļ‡āļˆāļ°āļ‚ึ้āļ™āļĄāļēāđ€āļ›็āļ™āļ­ัāļ™āļ”ัāļšāļ•้āļ™ āđ† āļ­āļĒ่āļēāļ‡āđāļ™่āļ™āļ­āļ™ āđ€āļžāļĢāļēāļ°āđ€āļ§็āļšāđāļŦ่āļ‡āļ™ี้āđ„āļ”้āđ€āļ›ิāļ”āđƒāļŦ้āļ™ัāļāļžāļ™ัāļ™āđ„āļ”้āļĢ่āļ§āļĄāļŠāļ™ุāļāđ„āļ›āļัāļšāļāļēāļĢ āđāļ—āļ‡āļšāļ­āļĨāđ‚āļĨāļ āđ„āļ”้āļ­āļĒ่āļēāļ‡āđ€āļ•็āļĄāļĢูāļ›āđāļšāļš āļ—ี่āļˆāļ°āļŠāļēāļĄāļēāļĢāļ–āđ€āļĨืāļ­āļāļ„ู่āđāļ‚่āļ‡āļ‚ัāļ™āļŠāļģāļŦāļĢัāļšāļāļēāļĢāđ€āļ”ิāļĄāļžัāļ™āđ„āļ”้āļ„āļĢāļšāļ—ุāļāđāļĄāļ•āļŠ์āļ•āļĨāļ­āļ”āļĪāļ”ูāļāļēāļĨ āļĄāļēāļžāļĢ้āļ­āļĄāļัāļšāļĢāļēāļ„āļēāļšāļ­āļĨāđ€āļžีāļĒāļ‡ 4 āļ•ัāļ‡āļ„์āđ€āļ—่āļēāļ™ั้āļ™ āļ­ีāļāļ—ั้āļ‡āļĒัāļ‡āđƒāļŦ้āļœู้āđ€āļĨ่āļ™āđ„āļ”้āļĨāļ‡āļ—ุāļ™āđ„āļ›āļัāļšāđ€āļ‡ิāļ™āđ€āļĢิ่āļĄāļ•้āļ™āđ€āļžีāļĒāļ‡ 10 āļšāļēāļ—āđ€āļ—่āļēāļ™ั้āļ™

    ReplyDelete
  39. āļ„āļēāļŠิāđ‚āļ™āļ­āļ­āļ™āđ„āļĨāļ™์āļĢāļ°āļ”ัāļšāļžāļĢีāđ€āļĄี่āļĒāļĄ āļ—ี่āļĄีāļŠāļēāļ§āļŠāļ§āļĒāļĄāļēāđāļˆāļāđ„āļž่ āļĢāļ°āļšāļšāļāļēāļ-āļ–āļ­āļ™ Auto 10 āļ§ิāļ™āļēāļ—ี āđ„āļĄ่āļ•้āļ­āļ‡āļĢāļ­āļ„āļ­āļĒ āļ„āļ­āļ™āđ€āļ‹āļ›āļ™ี้āļŠื่āļ­āļ§่āļē Sexy āļšāļēāļ„āļēāļĢ่āļē āļŦāļĢืāļ­āļ—ี่āđ€āļĢีāļĒāļāļ§่āļēāđ€āļĒ้āļēāļĒāļ§āļ™ āļžāļ§āļāđ€āļĢāļēāļĒ้āļģāļšāļĢิāļāļēāļĢāđƒāļŦ้āļœู้āđ€āļĨ่āļ™āđ„āļ”้āđ€āļˆāļ­āļัāļšāļāļēāļĢāđāļ›āļĨāļāđƒāļŦāļĄ่āļˆāļēāļāđ€āļ”ิāļĄāļ—ี่ PGSLOT

    ReplyDelete
  40. I enjoy your writing very much! Do you mind if we communicate with regards to your posts on AOL? I need an expert to solve my issue. Maybe you are the one!
    We are looking forward to seeing you. Astrologer In New York,

    ReplyDelete
  41. UFABET888 āļŠุāļ”āļĒāļ­āļ”āļœู้āđƒāļŦ้āļšāļĢิāļāļēāļĢāđ€āļāļĄāđ€āļ”ิāļĄāļžัāļ™āļ­āļ­āļ™āđ„āļĨāļ™์āļŠื่āļ­āļ”ัāļ‡āļ­āļĒ่āļēāļ‡āļŠāļĨ็āļ­āļ•āļ­āļ­āļ™āđ„āļĨāļ™์ āđ€āļ§็āļšāđ€āļ”ิāļĄāļžัāļ™āļ­āļ­āļ™āđ„āļĨāļ™์āļ—ี่āđ€āļ›็āļ™āļœู้āđƒāļ™āļ—āļēāļ‡āļ”้āļēāļ™āļŠāļģāļŦāļĢัāļšāđ€āļāļĄ āļŠāļĨ็āļ­āļ•āļ­āļ­āļ™āđ„āļĨāļ™์ āļ—ี่āļ™ัāļāļĨāļ‡āļ—ุāļ™āļŠāļēāļ§āļ•่āļēāļ‡āļŠāļēāļ•ิāļ—ั่āļ§āđ‚āļĨāļ āđ€āļĨืāļ­āļāđ€āļ‚้āļēāļĄāļēāđƒāļŠ้āļšāļĢิāļāļēāļĢāļĄāļēāļāļ—ี่āļŠุāļ”āđƒāļ™āđ€āļ­āđ€āļŠีāļĒ āļ™ี่āđ€āļ›็āļ™āđ€āļ§็āļš āļŠāļĨ็āļ­āļ•āļ­āļ­āļ™āđ„āļĨāļ™์āļ—ี่āļ”ีāļ—ี่āļŠุāļ”āđƒāļ™āđ€āļ­āđ€āļŠีāļĒ āļ—ี่āļĄีāļāļēāļĢāļ•้āļ­āļ™āļĢัāļšāđƒāļŦ้āļิāļˆāļāļĢāļĢāļĄāļ•่āļēāļ‡ āđ† āļĄāļēāļāļĄāļēāļĒāđƒāļŦ้āļัāļšāļ™ัāļāļžāļ™ัāļ™āļ­āļ­āļ™āđ„āļĨāļ™์āļŦāļ™้āļēāđƒāļŦāļĄ่āļ—ุāļāļ—่āļēāļ™ āđ„āļĄ่āļ§่āļēāļ—่āļ™āļēāļˆāļ°āļĄีāđ€āļ‡ิāļ™āđƒāļ™āļāļēāļĢāļĨāļ‡āļ—ุāļ™āđ€āļžื่āļ­āđ€āļĨ่āļ™āļžāļ™ัāļ™āļ­āļ­āļ™āđ„āļĨāļ™์āļĄāļēāļāļ™้āļ­āļĒāđ€āļžีāļĒāļ‡āđƒāļ” āļ—่āļēāļ™āļ็āļŠāļēāļĄāļēāļĢāļ–āđ€āļ‚้āļēāļĄāļēāđ€āļ”ิāļĄāļžัāļ™āđ„āļ”้āļ‡่āļēāļĒ āđ† āļัāļšāđ€āļ§็āļšāļžāļ™ัāļ™ UFABET888 āđ‚āļ”āļĒāļ—ี่āđ„āļĄ่āļ•้āļ­āļ‡āđƒāļŠ้āđ€āļ‡ิāļ™āļ‚āļ­āļ‡āļ•ัāļ§āđ€āļ­āļ‡āđƒāļ™āļāļēāļĢāļĨāļ‡āļ—ุāļ™

    ReplyDelete