I'm making a button that increments the value when press once and hold press the button. As long as the button is hold, the value will keep increasing. I've been told to use onTouch instead of onClick to do this, but couldn't find the right way to do so. The below code works for only press once.Thanks.
public class MainActivity extends AppCompatActivity {
int i = 0;
TextView textView;
Button button;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
textView=findViewById(R.id.textView);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP){
setText();
}
return true;
}
});
}//oncreate
public void setText(){
textView.setText(""+i);
i++;
}
}//class