i have a little question. How can i implement click, doble click and hold in a Button. I really need add this functions to my button. I have AndroidStudio 2,3,3. Thanks you!!
Asked
Active
Viewed 437 times
2 Answers
0
Your activity has to implement the following interfaces: View.OnClickListener
, View.OnLongClickListener
.
When you have your ButtonId defined like this: android:id="@+id/button"
:
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button: //what should happen when the button is pressed
break;
}
}
You also have to set the Listeners in onCreate
:
setOnClickListener(this);
setOnLongClickListener(this);
The code for onLongClick
looks exactly the same.
Double Tap is a bit more complicated, here you can find how to implement double tap.

sininen
- 503
- 1
- 6
- 20
0
button.setOnLongClickListener
and button.setOnClickListener
should do the trick for long and single clicks respectively.
For double tap here's what I do in the setOnClickListener.
boolean click=false;
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(click==true)
//DO SOMETHING
new Handler().postDelayed(new Runnable(){
public void run(){
click=true;
}, 1000};
});

Aniruddha Bera
- 399
- 6
- 19