I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}
Related
I have a button that runs a code that changes the background to a random color on each click. I would also like to give the user the ability to keep on changing the background color as long as the button is clicked. I believe that the onTouchListener will be my best bet. However, I do not now how to implement the code correctly.
I tried on the onLongClickListener but found out that onLongClickListener doesn't work that way.
Incomplete code for the onTouchListener (randomize is the name of my button):
randomize.setOnTouchListener(new Button.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// start the thread
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP){
// stop the thread
return true;
}
return false;
}
});
So, what I aim to be able to do is to keep on pressing the button and having the background continuously change while still preserving the onclick method of the button. So, onclick changes the background once and continuous click changes the background continuously.
Thank you so much folks :)
Ps. I'm just a beginner to android so I'm sorry if I do not know much. :)
try this:
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
count++;
Toast.makeText(getApplicationContext(),Integer.toString(count) , Toast.LENGTH_SHORT).show();
return true;
}
});
I have a mouse connected to my Android 6.0 tablet. I'm trying to listen to left and right mouse pressed and released events in my onTouch. It works fine when pressing mouse buttons, but when releasing mouse buttons getButtonState() always returns 0 so I cannot tell which button (left or right?) has been released.
Here is my code:
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getSource() == InputDevice.SOURCE_MOUSE) {
int action = event.getActionMasked();
int mouseButton = event.getButtonState();
Log.v("Test", "Action: " + action + " MouseButton: " + mouseButton);
return true;
}
return false;
}
When Action is 1 (i.e. ACTION_UP), mouseButton is always 0 so I cannot tell if the left or right mouse button has just been released. When Action is 0 (i.e. ACTION_DOWN), mouseButton is correctly set to BUTTON_PRIMARY or BUTTON_SECONDARY.
So does anybody have an idea how to distinguish between left and right mouse button releases on ACTION_UP?
To answer my own question, isButtonPressed() is the key to this problem. By monitoring isButtonPressed() and caching its result whenever you get a MotionEvent from InputDevice.SOURCE_MOUSE you can tell which button has been pressed or released. You also need to be careful not to limit those observations to ACTION_UP and ACTION_DOWN because when one button is already down and another one is pressed or released, you won't get ACTION_DOWN or ACTION_UP for it but an ACTION_MOVE event will be generated as soon as there's already one button down.
So in terms of code, a solution could look like this:
private static boolean mLeftDown = false, mRightDown = false, mMiddleDown = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getSource() == InputDevice.SOURCE_MOUSE) {
boolean leftDown = event.isButtonPressed(MotionEvent.BUTTON_PRIMARY);
boolean rightDown = event.isButtonPressed(MotionEvent.BUTTON_SECONDARY);
boolean middleDown = event.isButtonPressed(MotionEvent.BUTTON_TERTIARY);
if(leftDown != mLeftDown) {
// left button pressed or released
mLeftDown = leftDown;
}
if(rightDown != mRightDown) {
// right button pressed or released
mRightDown = rightDown;
}
if(middleDown != mMiddleDown) {
// middle button pressed or released
mMiddleDown = middleDown;
}
return true;
}
return false;
}
This works fine on Android 6.0.
Consider an Activity where button named "Scan", is disabled as soon as Bluetooth device gets connected, for preventing further scanning.
mScan.setEnabled(false);
But as soon as it is connected I need the same button mScan to show some dialog which has additional functionalities when it is Long Pressed.
The problem is as I disabled the mScan button I can't perform onLongClick function.
How Can I achieve the same?
Seeking your help.
You can use a custom OnTouchListener:
long lastDown;
long lastDuration;
public class YourOnTouchListener implements View.OnTouchListener
{
public YourOnTouchListener(some parameters) {
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
lastDuration = System.currentTimeMillis() - lastDown;
if (lastDuration > the duration you want) {
//do what you want here
return true;
}
return false;
}
}
Then you can control what you want to do according to a duration, and it works with a OnClickListener as well.
I have to develop one apps, in that i want to fire one action, like button is vibrate contentiously during press,i leave press it stop vibration.
In onTouch - event occur only during press,but when i press button contentiously -> event occur contentiously
like i press button for 1 minute it vibrate for same time,if leave press it stop.
i don't know which method used for this, so, please any one help me to do like this.
my code is below to do that : but it not work for continuous action during press (using thread it work for vibrate only, if i join more code with that it give error see in following.
**Edit : **
i got solution for vibrate using like below but when i write code (below vibrator.vibrate(100); ) for some animation that continuous during press then i got error : Only the original thread that created a view hierarchy can touch its views. i also try with runOnUIThread also but with that it not work.
img_laser_ballpen.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MainActivity.this.vibrating = true;
img_laser_light.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
public void run() {
while (MainActivity.this.vibrating) {
// ADD CODE FOR MAKING VIBRATION
vibrator.vibrate(100); // it works properly
//Animation shake = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.shake);
//img_laser_light.startAnimation(shake); //if it open then give error
}
//
}
}).start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
MainActivity.this.vibrating = false;
img_laser_light.setVisibility(View.INVISIBLE);
}
return vibrating;
}
});
You could like this
Button btn = (Button) findViewById(YOUR_BUTTON_ID);
btn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event){
if(event.getAction() == MotionEvent.DOWN){
// Start vibrating
}else if (event.getAction() == MotionEvent.UP){
// Stop vibrating
}
}
});
This answer has a good example of catching these events; namely - you should use the OnTouchListener instead of OnClickListener.
// this goes wherever you setup your button listener:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// START VIBRATE HERE
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// STOP VIBRATE HERE
return true;
}
}
};
Edit:
You should return true after handling your vibrate so that other events are fired for this view. From the documentation:
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
You could opt for the following:
Button btn = (Button) findViewById(YOUR_BUTTON_ID);
boolean vibrating = true;
btn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View view, MotionEvent event){
if(event.getAction() == MotionEvent.DOWN){
YOUR_CLASS_NAME.this.vibrating = true;
new Thread(
new Runnable(){
public void run(){
while(YOUR_CLASS_NAME.this.vibrating){
//ADD CODE FOR MAKING VIBRATION
}
}
}
).start();
}else if (event.getAction() == MotionEvent.UP){
YOUR_CLASS_NAME.this.vibrating = false;
}
}
});
Problem description:
I have a TextView on a RelativeLayout and I want to color it red when the user touches it, and go on another page when he clicks on it.
So I tried to set an OnClickListener to do the click, and an OnTouchListener to implement the touch function (MotionEvent.ACTION_DOWN) but this combination doesn't work, because OnTouchListener makes OnClickListener non-functional (don't know why).
On forums people say that we can implement the OnClick by the OnTouch MotionEvent.ACTION_UP, but this one can be triggered out of my TextView layout (the TextView gonna be clicked if you press it and drag your finger out of him to release) and this is not the desired behavior because I want:
click = press + release on the TextView.
Can someone give me a solution for this please?
you may call View.performClick() when action_up. Hope it helps.
your_txtView.setOnClickListener(new TextView.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
} else if (MotionEvent.ACTION_UP == event.getAction()) {
v.performClick();
}
return true;
}
});
Adel, is the problem with the first click, or you don't get any click at all?
There is this issue if you have multiple clickable layout you don't get any click events for the first. That's because it makes it first selected and then you get the click event, try the below code.
private class CustomTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
TextView tv = (TextView) v.findViewById(R.id.single_line_text);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
tv.setTextColor(COLOR_WHEN_PRESSED);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
tv.setTextColor(COLOR_WHEN_RELEASED);
// Action of click goes here
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
tv.setTextColor(COLOR_WHEN_RELEASED);
// To handle release outside the layout region
}
return false;
}
}
This is working in my current implementation if you set the touch listener for your layout.
You also need to set below on your layout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
Hope it helps!!!
EDIT: Additionally, there should be a flag in both DOWN and UP. Set it in DOWN and check if its set in UP. This will avoid a bug where user might tap anywhere in the screen and then hover on your textview and release it.
Had the same problem. Solved it by returning false from ACTION_MOVE. I've been fighting with it for few hours, trying various things, but seems like i've kept overlooking this little issue... And now it makes sense. When you return true from onTouch, futher processing is stopped, so that OnClickListener is not aware of any movements and triggers onClick even after pointer have moved outside of view.