I have a button defined in my layout, I implement onclick buttons without problems, but now I need to know when my button is doww and up, like onkeydown event for fisical buttons. There is something like that for customs buttons??. Because onKeyDown (int keyCode, KeyEvent event) have a int keycode but muy custom button haven't. I hope someone can take away that query. thank you in advance
Asked
Active
Viewed 2,794 times
2 Answers
7
Yes there is. try this code.
yourButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN: break;
case MotionEvent.ACTION_UP: break;
}
return true;
}
});

Shiv
- 1,269
- 11
- 20
0
I think, you can try something like this :
public class MainActivity extends Activity {
private Button button ;
private Boolean buttonState = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buttonState = !buttonState;
if(buttonState){
button.setText("Down");
} else {
button.setText("Up");
}
}
});
}
}

Yurii Buhryn
- 803
- 3
- 9
- 27
-
I dont think thats the correct answer He need touch events up and down.You are talking about toggle . – Shiv Nov 09 '12 at 16:29
-
thank "Yuriy Bugryn", "spanjeta" was right. But your code was helpful because I learned thing more – cristianego Nov 09 '12 at 18:39
-
ohh, really I Thought, that he want toggle ))) – Yurii Buhryn Nov 23 '12 at 13:01