Triggering event when Button is pressed down in Android
Maybe using a OnTouchListener
? I guess MotionEvent will have some methods for registering a touch on the object.
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}))
Triggering multiple buttons(onClick event) with one swipe/gesture
I was looking for a solution, and finally made it work. So I will answer here and hope it will help to someone.
I had a problem detecting a swipe with a layout with multiple buttons, so I override the method setOnTouchListener
for every button, calling inside gesturedetector.onTouchEvent(event)
(and do what you want to do with the event detected).
Obviously, to detect the button click event you only have to use setOnClickListener
method.
Maybe it's no the most elegant way to make this work, but at least worked for me.
Capture button release in Android
You should set an OnTouchListener on your button.
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
increaseSize();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
resetSize();
}
}
};
listener for pressing and releasing a button
You can use a onTouchListener
:
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
return true; // if you want to handle the touch event
}
return false;
}
});
Sliding finger across screen activating buttons as if they were pressed
I actually took the "easy" way out and used buttons with an onTouch-method using ACTION_DOWN
and ACTION_MOVE
with coordinate calculations that combined with event.getX()
and event.getY()
allows me to detect which button is currently hovered. It's lag free with 13 buttons.
Related Topics
Sqlite Query in Android to Count Rows
How to Fix "Fail to Connect to Camera Service" Exception in Android Emulator
How Can Android Source Code Not Have a Main Method and Still Run
Android.Content.Res.Resources$Notfoundexception: Unable to Find Resource Id #0Xffffffff
Creating a Dialogpreference from Xml
Does Retrofit Make Network Calls on Main Thread
Android Sax Parser Not Getting Full Text from Between Tags
Gradle Flavors for Android with Custom Source Sets - What Should the Gradle Files Look Like
How to Return String or JSONobject from Asynchronous Callback Using Retrofit
Intent Putextra Arraylist<Namevaluepair>
What Happens If a Android Service Is Started Multiple Times
How to Reduce the Spacing Between the Action Item Icons on Action Bar
How to Implement Pagination in Android Listview
Android Studio: Default Project Directory
Switching Between Fragment View