Home > Back-end >  How to make one button with two states of click android
How to make one button with two states of click android

Time:03-26

I am trying to make one button make 2 functions if oneClick to give me an ID and if long click to take me to another activity this is my code:

if(caseID == 1){

btn.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
oneClick();
}
});


btn.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

longClick();
return true
} 
});
}

They both work separately but together like this only the long click works while the onClick makes my app crash. Is is possible to be done for one button to have this 2 states together?

CodePudding user response:

Your code is correct just change this

if(caseID == 1){

btn.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
oneClick();
}
});


btn.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

longClick();
**return false** //from true make it false
} 
});
}
  • Related