How To Count Button Press Time In Android?
Solution 1:
I faced it since 3 month ago, here is my solution:
privateintcount=0;
Button btn;
privatefinalHandlermHandler=newHandler();
private Runnable mTimer1;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
mTimer1 = newRunnable() {
@Overridepublicvoidrun() {
count++;
// TODO Auto-generated method stub
mHandler.postDelayed(this, 500);
if (count == 1) {
mHandler.removeCallbacks(mTimer1);
// put your code here:
Intent i;
PackageManagermanager= getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.technorapper.technorappermapp");
if (i == null)
thrownewPackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
}
}
};
btn.setOnTouchListener(newOnTouchListener() {
privateint initialX;
privateint initialY;
privatefloat initialTouchX;
privatefloat initialTouchY;
@OverridepublicbooleanonTouch(View v, final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = (int) btn.getX();
initialY = (int) btn.getY();
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
mHandler.postDelayed(mTimer1, 0);
returntrue;
case MotionEvent.ACTION_UP:
count = 0;
returntrue;
case MotionEvent.ACTION_MOVE:
runOnUiThread(newRunnable() {
publicvoidrun() {
btn.setX(initialX
+ (int) (event.getRawX() - initialTouchX));
btn.setY(initialY
+ (int) (event.getRawY() - initialTouchY));
}
});
returntrue;
}
returnfalse;
}
});
with this code you can move your chathead and the event start after 500 milisecond. No need to use OnClickListener, check if position not change so detect it as OnClickListener. And if you want to stop OnTouch event when catch some action, just put 'boolean variable' in ACTION_MOVE
Solution 2:
You can use View.OnTouchListener and two timestamp to store the button click start (ACTION_DOWN) and end time (ACTION_UP). Then, difference between this 2 variables (end-start) will tell you the duration of button click time and you can execute code according to your preference.
test.setOnTouchListener(new OnTouchListener() {
privatelong start = 0;
privatelong end = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
this.start = System.currentTimeMillis();
} elseif (event.getaction()==MotionEvent.ACTION_UP) {
this.end = System.currentTimeMillis();
}
}
});
Solution 3:
If you want do have custom durations you have to program it by yourself.
However it is recommended to use the android build in functions. There are a View.OnLongClickListener and a View.OnClickListener to distinguish from two different durations for clicking on views.
Solution 4:
i can't tell you the full exact code but i can explain the flow here, what i get from your answer is that you need to do different actions on the basis of last time the button is pressed, this is how you can do. You need two Date variables i.e. current and lastTime
DateTimeUtils.getCurrentUTCFormattedDateTime()
store the lastTime in preferences on action down fetch the last time from preferences and get the current time and subtract it, to get the difference.
long difference = (current.getTime() - lastSaved.getTime()) / 1000;
give you time difference in seconds, then you decide what to do. Hope it help :)
Post a Comment for "How To Count Button Press Time In Android?"