I want to create an Android application to control a car remotely using wifi.
I discovered something pretty weird about ontouch events, if I keep touching the screen, event if I touch another button the event will call the first view, because I don't release the screen.
So, if I touch the FORWARD button, if I try to go left/right at the same time, I can't, because the event called is always forward if I don't relase the button. I have to stop touch the screen and touch left/right button to change direction.
How can I manage that?
Thank you.
// On click, go forward.
this.goForwardBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goForward();
}
});
This happens when your Activity implements onTouchListener. You can define a view that does not cover the entire screen and let it implement onTouchListener.
Related
I want to simulate the iOS 3D Touch. I have a recyclerview, where i register a OnLongClickListener to every item. When a longClick is detected, a AlertDialog opens.
For checking if the longClick ends, i have implemented the OnTouchlistener.
In short, i implemented it like it was described here: https://stackoverflow.com/a/10746549/4907047
It works like a charm, as long as i don't move the finger after longClick. If i move the finger, the listview under the dialog is still moving with my finger. Besides that the dialog will not close after stop touching the screen.
I think i have to cancel the events of the underlying listview. Does anyone know, how to handle this?
You should consider implementing a contextMenu since it can do what you want. Read this.
I am trying to drag a view from one place to another and also have a single click/tap to do a completely different action without dragging the view.
Right now what I have a long click listener to start my drag like this
view.setOnLongClickListener(..
....
view.startDrag(...
And for my single click I use a click listener like this
view.setOnClickListener(...
// My completely different action
How can I have it so that I don't have to use long click to start drag, but can start drag by allowing user to just simply move his/her finger from my view to the target.
Use view.setOnTouchListener(..
the function will be called as soon as you touch the item. In this function, you should call your view.startDrag(... function and you are good to go.
Have fun and Take Care.
use a delay thread inside on touch listner
because on click works when you remove your finger dragging cant be done after removing finger
Let's assume there are all kinds of views on screen, is there any way to get every click/touch event on whole screen? And obviously I don't want to override every view's click/touch event since maybe I don't even know what views we will have.
I found that you can't even get events behind a Button.
Like I setOnClickEvent in LinearLayout which is the whole background of screen, then add a Button above this LinearLayout, when click this Button, Mr. Jon LinearLayout Snow knows nothing.
You can override this method in your activity.
#Override
public void onUserInteraction() {
super.onUserInteraction();
}
Every touch event on opened activity will fire this method.
I am making a simple game that displays balloons (Button/ImageView with background pic of a balloon). I've animated the balloons using the anim.xml files and the balloons move pretty well around the screen.
The problem is that when you touch them, they don't do anything. The location I placed them in XML retains the power to click on them. On touching the moving pics, nothing happens.
How do I make the balloons pop on touch (i.e. change background to a broken balloon image and produce a sound)?
Refer this link to know the property animation working. also use object animator to animate view. so only the click functionality of the view will remain in the view wherever we move.
Don't use animation XML files for this, use ViewPropertyAnimator instead. Just call `.animate()' on your balloon view and set a direction and duration of the animation. For example this code will move your balloon by 200 pixels to the right over 1 second:
balloonView.animate().translationXBy(200).setDuration(1000);
Check this link out.
This is an example of how to create views which are moving and can react on touches.
This is part of Coursera Android Development course.
If you are interested - take part in that course for free.
Best
Yes you create a SeperateClass For touch and Click listener and pass the Necessarydata.
e.g.
class BaloonClickListener implements onCLickListener
{
Button button;
public BaloonClickListener(Button button)
{
this.button=button;
}
#override
public void onCLick(View view)
{
button.setBackgroundResource(bustedImage);
}
}
Set Listener:
balloonView.setOnClickListener(new BaloonClickListener(balloonView));
I think this may solve your problem.
I have 4 buttons on an activity. I have set Touch Listeners for all the buttons.
Button1.setOnTouchListener(this);
Button2.setOnTouchListener(this);
Button3.setOnTouchListener(this);
Button4.setOnTouchListener(this);
All I want to do is to get MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP when I am moving my finger over the buttons.
I am getting these events when I am touching INDIVIDUAL buttons, but not when I move my finger from one button to another.
In this case, first button should get ACTION_UP message and the next button should get ACTION_DOWN message.
Kindly help.
There is a certain logic in such behavior of touchevents.
I think the best decision for you will be to set OnTouchListener to parent view of Button1, Button2, etc or override its OnTouchEvent() method. After that you will be able to send MotionEvent to child views (buttons in your case) manually when it's necessary.
My initial instinct is that you are sending all of the touch events to the same function, causing some type of bottleneck.
What if you seperate the touch handlers into four seperate functions like this - that way, all buttons have independent touch handlers and there is no collission by having them all using the same function to handle their collective events. Make a seperate "onTouchEventListener" for each button and have it look like this:
Button1.setOnTouchListener(button1Listener);
Button2.setOnTouchListener(button2Listener);
Button3.setOnTouchListener(button3Listener);
Button4.setOnTouchListener(button4Listener);
Here is how to do it from the Android docs:
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
Since you're trying to capture a MotionEvent that involves all of the buttons, as opposed to just one, you will have to capture the MotionEvents in the parent view, then dispatch them to children buttons in the form of MotionEvent.ACTION_DOWN followed by MotionEvent.ACTION_UP.
I did something similar to make a multitouch keyboard out of a RelativeLayout containing many buttons. I basically overrided dispatchTouchEvent and kept track of each finger, sending ACITON_DOWN and ACTION_UP in place of ACTION_POINTER_DOWN and ACTION_POINTER_UP for secondary fingers to children.
It may be useful to look at the source of ViewGroup and see how it dispatches events to its children, and tweak the behavior to check when the finger goes out of child button bounds, and if so, send an ACTION_UP on the old button then an ACTION_DOWN on the new button.