'Three type of action on single button
I am creating a simple counting app when the button pressed on button ACTION_DOWN and ACTION_UP store the time. When time difference is less than 300 ms increment one time or countdown difference time and increment one. I like to stop the countdown on the next click on the same button. How can I do this?
package com.example.myapplication_test_01;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
CountDownTimer countDownTimer;
TextView mTextField ;
Button close ,count;
private long time1 ,time2;
private long timeDifferance;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextField = findViewById(R.id.outtext);
close = findViewById(R.id.close);
count = findViewById(R.id.count);
count.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch ( motionEvent.getAction() ) {
case MotionEvent.ACTION_DOWN:
time1 = System.currentTimeMillis();
return true;
case MotionEvent.ACTION_UP:
time2 = System.currentTimeMillis();
timeDifferance = time2 - time1;
if(timeDifferance < 300){
// take as a click and increment count one time
Log.d("T","its a click" );
}else {
// start a auto counting use the timeDifferance
countDownTimer = new CountDownTimer(timeDifferance,1000) {
@Override
public void onTick(long millisUntilFinished) {
mTextField.setText("count " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
mTextField.setText("done ! " );
// each time increment once and start countdown again until one more click
countDownTimer.start();
}
}.start();
}
return true;
}
return false;
}
});
// close.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
//
// countDownTimer.cancel();
// mTextField.setText("done ! " );
// }
// });
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/outtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.91"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.358" />
<Button
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.689" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
