To detect Touch Event and other events like Left to Right sweep and Right to Left sweep, we use MotionEvent class.
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
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
<?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>
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;
}
}
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
<?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;
}
}
thanks very simple and good example
ReplyDeletegood example sir!!!
ReplyDeletegood example sir!!!
ReplyDeleteVery Good Example Sir But How Do You Do It Using A Image i.e., Sliding a Image and Viewing A Text ??/
ReplyDeleteGood But How To Swipe With A Image To Top To Display Something ??? Please Help Out !!!
ReplyDeleteIt's simple but sometimes left-right swap and down-up swap both performs, tell me why?
ReplyDeleteIf 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;
Deletefloat 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))
Hi sir how exactly are we going to add the if statements? could you please provide a sample. thank you
Deletedown 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
DeleteVery 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?
ReplyDeleteVery good example, but if we want to check diagonal? we just combine for example the up and left at the same time?
ReplyDeletethat's very good example but tost is running which not required at one action also perform downup and updaown in this example.........
ReplyDeleteGreat tutorial with clear explanation.
ReplyDeleteSimple is best, Thank you.
ReplyDeletehow can we get on click () including this all...
ReplyDeletei want to handle onclick on listitem too,among these all events... how can i do that
ReplyDeleteThank you, very simple, very clear.
ReplyDeleteThank you, very simple, very clear.
ReplyDeletevery simple and clear.. thanks alot and keep it up
ReplyDeleteThanks for appreciation.
DeleteNeat! +1
ReplyDeletegood example... bt what if we wnt to swipe to another page? how can we do this?
ReplyDeleteIf i want to just swipe horiztonally, how do I accomplish this? I tried to disable "y" but it still allows swipe vertically.
ReplyDeleteThanks ! That's great.
ReplyDeleteHow can i enable scrolling Y axis with this code?
ReplyDeleteHello,
ReplyDeleteif (x1 > x2)
{
Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
How to change TextView
}
First give id to TextView of Detect Swap Event Demo in main.xml
Deleteand 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");
}
Glad to see fuuastians reply :) (Y)
DeleteVery Good example, buddy.
ReplyDeleteVery Helpful for beginners!!
ReplyDeletenice bro!
ReplyDeleteits work fine here.
thank you!
very nice sir,thank you so much!!!
ReplyDeleteGOOD
ReplyDeletenice tutorial
ReplyDeleteThanks it help us very much
ReplyDeleteThaks A LOT. It REALLY helps me.
ReplyDeleteððððð
ReplyDeletethaaaaaanks alooooot
ReplyDeletei searched for this method about one weak but i didnt find any thing
thank you very much
Really nice,clear explanation got..,Thanks
ReplyDeleteReally nice,clear explanation,simple code,thanks
ReplyDeletethank you so much so helpful
ReplyDeletenice article
ReplyDeleteHello, your article is very nice...
ReplyDeleteif(x1>x2)
{
How to highlight textview part from x1 to x2?
}
hello .. in above snippet , if i want to highlight that textview contain on which swapping has been performed , so how can i do that?
ReplyDeletehowever 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
ReplyDeleteRead Shayari in Hindi, Best & New Hindi Shayari on Love, Sad, Funny, Friendship, Bewafai, Dard, GoodMorning, GoodNight, Judai, Mehakal, Whatsapp Status in Hindi & English
ReplyDeleteShayari By Categories:-
Ab Shayari.Guru
MAHAKAL STATUS
MOTIVATIONAL QUOTES
ATTITUDE STATUS
BEWAFA SHAYARI
SHARABI SHAYARI
Pls rewrite to Kotlin
ReplyDeleteAwesome and a great value article. Really nice really good one
ReplyDeletelovely ki shayari
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,
ReplyDeleteI 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, |
ReplyDeleteThe Single Best Strategy To Use For Top Astrologer In Sydney,
āđāļāļัāļāļุāļัāļāļี่āļุāļāļāļ่āļēāļāļ็āļĄāļāļāļŦāļēāļŠิ่āļāļี่āļāļ°āļāļģāļāļ§āļĒāļāļ§āļēāļĄāļŠāļ°āļāļ§āļāđāļŦ้āļัāļāļัāļ§āđāļāļ āđāļāļĢāļēāļ°āđāļāļāđāļāđāļĨāļĒีāļ่āļēāļāđ āļี่āđāļ้āļēāļĄāļēāļĄีāļŠ่āļ§āļāļŠāļģāļัāļāđāļāļีāļ§ิāļ āđāļāļĒāđāļāļāļēāļ°āđāļāđāļĢื่āļāļāļāļāļ āđāļāļāļāļāļĨ āļึ่āļāļāļ°āļีāļāļ§่āļēāđāļŦāļĄ āļŦāļēāļāļ่āļēāļāđāļ้āđāļิāļĄāļัāļāļāļĒู่āļี่āļ้āļēāļ āļ่āļēāļāļāļēāļāđāļāļĢāļĻัāļāļ์āļĄืāļāļืāļ āđāļ้āļāļĨāļāļ 24 āļั่āļ§āđāļĄāļ āļัāļāđāļ§็āļāļāļัāļ UFA888 āđāļ็āļāđāļ§็āļāļāļัāļāļāļāļĨāļāļāļāđāļĨāļ์āļŠāļēāļĄāļēāļĢāļāļāļāļāđāļāļāļĒ์āļ่āļāļāļ§āļēāļĄāļ้āļāļāļāļēāļĢāļāļāļāļู้āļāļ āđāļĨāļ°āļāļģāđāļŦ้āđāļ้āļĢัāļāļāļ§āļēāļĄāļŠāļ°āļāļ§āļāļŠāļāļēāļĒāļĄāļēāļāļāļ§่āļēāđāļāļŦāļĨāļēāļĒ āđ āļ้āļēāļ āļีāļāļั้āļāļĒัāļāļŠāļēāļĄāļēāļĢāļāļŠāļĢ้āļēāļāļĢāļēāļĒāđāļ้ āđāļ้āļีāļāļ§่āļēāļāļēāļĢāđāļิāļĄāļัāļāđāļāļāđāļิāļĄ āđ āđāļāļĒāđāļāļāļēāļ°āļี่āđāļ§็āļāļāļāļāđāļĢāļē āļี่āļĄีāļัāļāļĢāļēāļāļēāļĢāļ่āļēāļĒāļāļĨāļāļģāđāļĢāļี่āļĄāļēāļāļāļ§่āļēāļāļēāļĄāđāļ๊āļ°āļāļāļĨ āļŦāļĢืāļāđāļ§็āļāļื่āļ āđ āļึāļ 3 āđāļ่āļēāđāļĨāļĒāļีāđāļีāļĒāļ§
ReplyDeleteāļŦāļēāļāļูāļāļึāļāđāļ§็āļāđāļāļ์āļี่āđāļŦāļĄāļēāļ°āđāļ่āļāļēāļĢāđāļิāļĄāļัāļ āļāļāļĨāđāļĨāļ 2022 āļĄāļēāļāļี่āļŠุāļ āļื่āļāļāļāļāđāļ§็āļ UFA888 āļ็āļāļāļāļ°āļึ้āļāļĄāļēāđāļ็āļāļัāļāļัāļāļ้āļ āđ āļāļĒ่āļēāļāđāļ่āļāļāļ āđāļāļĢāļēāļ°āđāļ§็āļāđāļŦ่āļāļี้āđāļ้āđāļิāļāđāļŦ้āļัāļāļāļัāļāđāļ้āļĢ่āļ§āļĄāļŠāļุāļāđāļāļัāļāļāļēāļĢ āđāļāļāļāļāļĨāđāļĨāļ āđāļ้āļāļĒ่āļēāļāđāļ็āļĄāļĢูāļāđāļāļ āļี่āļāļ°āļŠāļēāļĄāļēāļĢāļāđāļĨืāļāļāļู่āđāļ่āļāļัāļāļŠāļģāļŦāļĢัāļāļāļēāļĢāđāļิāļĄāļัāļāđāļ้āļāļĢāļāļุāļāđāļĄāļāļ์āļāļĨāļāļāļĪāļูāļāļēāļĨ āļĄāļēāļāļĢ้āļāļĄāļัāļāļĢāļēāļāļēāļāļāļĨāđāļีāļĒāļ 4 āļัāļāļ์āđāļ่āļēāļั้āļ āļีāļāļั้āļāļĒัāļāđāļŦ้āļู้āđāļĨ่āļāđāļ้āļĨāļāļุāļāđāļāļัāļāđāļิāļāđāļĢิ่āļĄāļ้āļāđāļีāļĒāļ 10 āļāļēāļāđāļ่āļēāļั้āļ
ReplyDeleteāļāļēāļŠิāđāļāļāļāļāđāļĨāļ์āļĢāļ°āļัāļāļāļĢีāđāļĄี่āļĒāļĄ āļี่āļĄีāļŠāļēāļ§āļŠāļ§āļĒāļĄāļēāđāļāļāđāļ่ āļĢāļ°āļāļāļāļēāļ-āļāļāļ Auto 10 āļ§ิāļāļēāļี āđāļĄ่āļ้āļāļāļĢāļāļāļāļĒ āļāļāļāđāļāļāļี้āļื่āļāļ§่āļē Sexy āļāļēāļāļēāļĢ่āļē āļŦāļĢืāļāļี่āđāļĢีāļĒāļāļ§่āļēāđāļĒ้āļēāļĒāļ§āļ āļāļ§āļāđāļĢāļēāļĒ้āļģāļāļĢิāļāļēāļĢāđāļŦ้āļู้āđāļĨ่āļāđāļ้āđāļāļāļัāļāļāļēāļĢāđāļāļĨāļāđāļŦāļĄ่āļāļēāļāđāļิāļĄāļี่ PGSLOT
ReplyDeleteI 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!
ReplyDeleteWe are looking forward to seeing you. Astrologer In New York,
UFABET888 āļŠุāļāļĒāļāļāļู้āđāļŦ้āļāļĢิāļāļēāļĢāđāļāļĄāđāļิāļĄāļัāļāļāļāļāđāļĨāļ์āļื่āļāļัāļāļāļĒ่āļēāļāļŠāļĨ็āļāļāļāļāļāđāļĨāļ์ āđāļ§็āļāđāļิāļĄāļัāļāļāļāļāđāļĨāļ์āļี่āđāļ็āļāļู้āđāļāļāļēāļāļ้āļēāļāļŠāļģāļŦāļĢัāļāđāļāļĄ āļŠāļĨ็āļāļāļāļāļāđāļĨāļ์ āļี่āļัāļāļĨāļāļุāļāļāļēāļ§āļ่āļēāļāļāļēāļิāļั่āļ§āđāļĨāļ āđāļĨืāļāļāđāļ้āļēāļĄāļēāđāļ้āļāļĢิāļāļēāļĢāļĄāļēāļāļี่āļŠุāļāđāļāđāļāđāļีāļĒ āļี่āđāļ็āļāđāļ§็āļ āļŠāļĨ็āļāļāļāļāļāđāļĨāļ์āļี่āļีāļี่āļŠุāļāđāļāđāļāđāļีāļĒ āļี่āļĄีāļāļēāļĢāļ้āļāļāļĢัāļāđāļŦ้āļิāļāļāļĢāļĢāļĄāļ่āļēāļ āđ āļĄāļēāļāļĄāļēāļĒāđāļŦ้āļัāļāļัāļāļāļัāļāļāļāļāđāļĨāļ์āļŦāļ้āļēāđāļŦāļĄ่āļุāļāļ่āļēāļ āđāļĄ่āļ§่āļēāļ่āļāļēāļāļ°āļĄีāđāļิāļāđāļāļāļēāļĢāļĨāļāļุāļāđāļื่āļāđāļĨ่āļāļāļัāļāļāļāļāđāļĨāļ์āļĄāļēāļāļ้āļāļĒāđāļีāļĒāļāđāļ āļ่āļēāļāļ็āļŠāļēāļĄāļēāļĢāļāđāļ้āļēāļĄāļēāđāļิāļĄāļัāļāđāļ้āļ่āļēāļĒ āđ āļัāļāđāļ§็āļāļāļัāļ UFABET888 āđāļāļĒāļี่āđāļĄ่āļ้āļāļāđāļ้āđāļิāļāļāļāļāļัāļ§āđāļāļāđāļāļāļēāļĢāļĨāļāļุāļ
ReplyDelete