I am using the default project template from Android Studio. When I swipe on the textView, the onClick
event will get triggered. Is it the designed behavior or where did I do something wrong?
Asked
Active
Viewed 797 times
0

Faysal Ahmed
- 7,501
- 5
- 28
- 50
-
How exactly gets clicked ? You have any listener set on the text ? – Ionut J. Bejan Apr 30 '18 at 09:45
-
yes, I set up a onclick listener on the textview. If I move finger from left to right or right to left, the onclick event will get trigger just the same way as it will get trigger with a single tap. – Apr 30 '18 at 09:48
-
I think this is how it should happen. If you really want to `fix` this behavior you should take a look at `TouchListener` and `do same stuff as onClick` but inside `ACTION_DOWN`. Anyway, unless you are going to have more complex actions there, I don't think this bothers a lot :) – Ionut J. Bejan Apr 30 '18 at 09:51
-
I would say that this is quite normal, but depends of your implementation. Show some code. If you have some mechanism to consume click in the case of swipe, as for example `ViewPager` than click will not be called on that TextView. – Gotiasits Apr 30 '18 at 09:52
-
@lonut, thanks for your suggestion. But I want to figure out if it is the default behavior of a textview or if it is only my problem? Will this happen on your Android Studio? – Apr 30 '18 at 09:54
-
1to understand the problem, you have to think about *what a click event is*. So it is: finger down + finger up. So basically the swipe is the same finger down + **finger move** + finger up. This means that each swipe is a click. – Vladyslav Matviienko Apr 30 '18 at 09:55
-
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("textview", "clicked"); } }); } – Apr 30 '18 at 09:55
-
@Vladyslav, so it is the default behavior that both swipe and single tap will trigger the View.OnClickListener event? – Apr 30 '18 at 09:57
-
yes. I do detect swipes manually with onTouch listener by checking the diff between finger down x/y and finger up x/y, but probably there are better ways. – Vladyslav Matviienko Apr 30 '18 at 09:58
-
using onTouch to detect click and swipe is really troublesome and complex. – Apr 30 '18 at 10:01
2 Answers
1
In my experience OnClick
is triggered because when you put your finger on textView
its ACTION_DOWN
method gets called internally and after swipe when you remove your finger its ACTION_UP
method gets called internally. So whenever these two combination gets called it calls the onClick
method. So when you touch (ACTION_DOWN
) the textView
and swipe left/right and move your finger outside (without the ACTION_UP
event) it won't get called because its ACTION_UP
is not called.
PS. This is not the official definition/working of onClick
... This is how it worked in my experience.

Somesh Kumar
- 8,088
- 4
- 33
- 49
-
the bigger problem is that I cannot implement the swipe gesture on the parent ViewGroup while implement onClicklistener on the textview correctly. – Apr 30 '18 at 10:00
-
I think this is a separate question... This is just the working of `onClick`... you can always implement `onTouch` of textView. There are plenty of answer to your problem (what you mentioned in comments). – Somesh Kumar Apr 30 '18 at 10:06
-
@StoneLai check [this](https://stackoverflow.com/questions/30745344/detecting-swipe-and-tap-gestures) out.. – Somesh Kumar Apr 30 '18 at 10:08
0
my solution is extending the parent viewgroup and examining motionevent in the onInterceptTouchEvent.