Issue
i want to execute simple click listener as well as long click listener on same button. But i need to execute long click listener after 5 seconds delay in the longclicklistener executes after 1 seconds of hold .so using handler it will executes after 5 seocnds.but i need exact to press button for 5 seconds then code to executes ...
Solution
you can use Handler
like this way:
Button b=findViewById(R.id.btn);
final Runnable run = new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
// Your code to run on long click
}
};
final Handler handel = new Handler();
b.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
handel.postDelayed(run, 5000/* OR the amount of time you want */);
break;
default:
handel.removeCallbacks(run);
break;
}
return true;
}
});
Answered By - Adil
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.