How can I detect clicked, pressed and released states of a Button. I want to perform different functions on these states. On click I want to call function1, on press I want to call function2 and on receive I want to call function3.
We can detect click state using View.OnClickListener. We can detect Pressed and Released states of a Button using View.OnTouchListener and handling ACTION_DOWN and ACTION_UP. I am able to detect these states individually, however, not together.
Below is code for OnCLickListener.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(" clicked ");
}
});
Below is code for OnTouchListener.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(" pressed ");
return true;
case MotionEvent.ACTION_UP:
System.out.println(" released ");
return true;
}
return false;
}
});
When I set click and touch listeners on a Button, Click event never gets called. Instead I receive pressed and released state.
How can I handle these three states together?
EDIT:
I added the OnClickListener and OnTouchListener code I have used.
Easy since Button is a View:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// Released
}
return true;
}
});
Change the return true; inside case MotionEvent.ACTION_DOWN: and case MotionEvent.ACTION_UP:to return false; or break;
See this link.
You can handle click manually in MotionEvent.ACTION_UP event by
button.performClick();
clicked event include pressed and released state,if you want to fire clicked event,put method after ACTION_UP
Related
I have an imageButton I want to play a music when, after a long press on my button , I remove hand from the button, then it will play music ,until I press another button , to stop it,
You can use an onTouchListener for this purpose:
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED : Play the music here
return true; // if you want to handle the touch event
}
return false;
}
});
I have a button that I would like clickable only by a stylus.
I used the method setClickable to enable or disable the click on the button, but how can I do that is clickable only with a pen?
the button should be clickable only when MotionEvent.TOOL_TYPE_STYLUS
how do i can?
you could override the Button's onTouchListener, and return immediately it the touch event is not performed through TOOL_TYPE_STYLUS. To retrieve this information, you can use
getToolType(int pointerX)
From the documentation
Gets the tool type of a pointer for the given pointer index
if it returns TOOL_TYPE_STYLUS, then you can simply check the MotionEvent for the ACTION_DOWN/ACTION_UP, and call performClick(). E.g.
b.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS) {
if (event.getAction() == MotionEvent.ACTION_UP ) {
performClick();
return true;
}
}
return false;
}
});
The most common way to handle a button click is:
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//exeute some code here
}
});
So I click the button, hold for a while and it executes the code when my finger actually leaves the button. When I hold it, I can't click it anymore (I mean while clicking - nothing happens).
1) Is it possible to execute the code when I my finger touches the button (not when it leaves)?
2) Is it possible to execute the code when I hold the button and then my second finger touches it (I want to use multitouch feature)?
As #Raghunandan commented, use setOnTouchListener as follow..
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
Log.e(TAG,"Down");
return true;
}
if (event.getAction()==MotionEvent.ACTION_MOVE){
Log.e(TAG,"Move");
return true;
}
if (event.getAction()==MotionEvent.ACTION_UP){
Log.e(TAG,"Up");
return true;
}
return false;
}
});
I want to keep a button displayed pressed. I want to use another way instead of onPressed() in onTouch(). I add setPressed(), setSelected() but not worked. when i add these methods in onTouch() the program is good but my animation is very slow.
Can i use these method in onClick() method but works in this?
Please explain for me
Instead of using the onClick-event which returns the button to the non-pressed state, you can set the pressed state in the onTouch-event:
yourbutton.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
((Button) v).setPressed(true);
//TODO: Add the code of your onClick-event here
}
return true;//Return true, so there will be no onClick-event
}
});
If this affects any animation, you should look for the problems there.
This piece of code makes the button work as a toggle button. It sets the pressed property to its opposite.
If the button is pressed, it's set as unpressed.
If the button is unpressed, it's set as pressed.
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Button b = ((Button) v);
b.setPressed(!b.isPressed());//if pressed, unpress; if unpressed, press
}
return true;
}
What you are describing seems to be a toggle button. Maybe you should check this out, instead of doing a hackish solution :
Toggle Buttons
Extend ToggleButton or:
Button button = (Button) view.findViewById(R.id.button1);
button.setTag(false);
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
if ((boolean) v.getTag()) {
v.setPressed(false);
v.setTag(false);
return false;
} else {
v.setPressed(true);
v.setTag(true);
return true;
}
return false;
}
});
I need to be able to tell when the user is holding a button down and when the user lets go. This is different from onClickListener and onLongClickListener. How would i go about doing something like this?
For example I press a button that starts a chronometer.(pseudo code)
if ButtonIsBeingPressed
{
chronometer start(); //and keep going
}
else chronometer stop();
//or on release or something
}
Look into the OnTouchListener it has MotionEvents for Down (press) and Up (release):
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Start
break;
case MotionEvent.ACTION_UP:
// End
break;
}
return false;
}
});