Reputation: 1580
I am developing a android app, this is a requirement. I need different functionality on onclick and also different functionality on stop and start method of ontouch. can any one help me? How can i implement this feature in android?
Upvotes: 1
Views: 56
Reputation: 26
I understand your problem and i hope below code will be help you.
public class MyFragment extends BaseFragment implements View.OnTouchListener {
@Override
public boolean onTouch(final View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
prev_timestamp = System.currentTimeMillis();
case MotionEvent.ACTION_UP:
current_timestamp = System.currentTimeMillis();
upcount++;
if (current_timestamp - prev_timestamp < 250) {
if (upcount == 2) {
upcount = 0;
Log.d(TAG, "click event");
//here code for onClick event
} else {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (upcount != 0) {
TOUCH_END=false;
Log.d(TAG, "touch start");
//here code for onTouchStart event
}
super.onPostExecute(aVoid);
}
}.execute();
}
} else {
upcount = 0;
Log.d(TAG, "touch end");
//here code for onTouchEnd event
}
case MotionEvent.ACTION_CANCEL:
//here code for cancel eent if you want
}
return true;
}
}
Source :see example here
Upvotes: 1