I have set OnTouchListener to an image view. I want to do two different actions when user press down the image and also when press up the image. But always I am getting press down event as (ACTION_DOWN) but if i use button instead of ImageView then I can both ACTION_DOWN and ACTION_UP. But I have to use ImageView not Button. How to achieve ACTION_UP event in ImageView
prevImage.setOnTouchListener(this); // prevImage is an image View
#Override
public boolean onTouch(View view, MotionEvent arg1) {
// TODO Auto-generated method stub
Log.i("Wallpaper","...On touch ..." + arg1.getAction());
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
//set img1 as imagesource
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
//set img2 as imagesource and parse data from url
}
return false;
}
Thanks
Don't know if this is too late or not.
You can do it by adding this to your imageView in your xml file
android:clickable="true"
and you can use if and else if like you already did there.
hope this helps.
Please check if ACTION_CANCEL is fired instead of ACTION_UP. It may get fired instead of ACTION_UP when it comes to images.
[EDIT] Also, you have to return true if you intercept ACTION_DOWN event in order ACTION_UP to get fired.
Related
Is it possible to know in a ListView if a item is pressed / touched (but not clicked) and know which?
I tried with "OnTouchListener" but without success (I can intercept UP and Down events on the ListView but not on the elements).
I tried also "OnItemLongClickListener" but I have to wait when I want information immediately.
Do you have an idea ?
Thank you in advance.
Edit (solution) :
I put the following code in my adapter in the item view.
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
image.setImageResource(R.drawable.image2);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
image.setImageResource(R.drawable.image1);
}
return true;
}
});
And now, when I touch an item, the picture becomes "image2" and when I do not touch the screen, it returns to "image1".
But there is a problem, if I press an item (the image2 appears well) and I move my finger in the list view and I do not touch the screen, it stays on for the image2, "MotionEvent.ACTION_UP" could not execute.
Do you have any idea how to do that as soon as I do not touch the screen, it must return on image1?
Thank you for your help.
You can set SetOnTouchListener to item view when your adapter create it.
you just try yo implement the Listener then you can override the function or please add your rough work part
I got an image on which I set OnTouchListener. When the person moves his finger away from the image, I need to check in which view he lifted the finger(or at least whether he was in same image when lifted finger). I read this and the answer was based on pixel color. Isn't there any way I can get the id of the view in which I lifted the finger? Or some flag which tells me whether I was in same view on which I implemented the OnTouchListener?
This is a little code in which I tried to compare the id of view on ACTION_UP, but it appears to be the same on which I implemented the listener....
tmw2 = (ImageView) findViewById(R.id.temp2);
tmw2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_UP) {
if (arg0.getId() == R.id.temp2)
tmw2.setImageResource(R.drawable.downloads_pressed);
}
return false;
}
});
I have an App with 10 Buttons. Everytime the User presses on one Button A TextView should change. And if the User changes the focus and ,oves its finger to right, to the next button(without taking the finger off the screen) the seond button should be focused.
I tried it with setting an OnTouchListner to all buttons, but once the finger is moved the focus still stays on the first button:
btn1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
txt1.settext("1");
return false;
}
});
I hope you understood what I mean and can help me with this.
Thanks
Edit:
I found an Application which does that, here is a Video, so you can visualise what I mean.
Notice how I move the mouse(the finger in this case) and the Boxes change its focus:
http://www.youtube.com/watch?v=MRVFpNrBmsA&feature=youtu.be
To start in your onTouch() you should handle the events...
Something like
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
txt1.settext("1");
} else if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT){
txt1.settext("0");
}
return true;
}
Not sure if those are the proper actions to check against but it is just to give the idea.
Also I believe you should return true otherwise it means that the event wasn't processed and it gets stuck.
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.
I have a view group that contains an image view. The view group is clickable and launches another activity. I want to set a selector for the image view, so that it changes its source image when sele cted, but I don want the image view to intercept the click event. I want the view group to deal with the event. The image view should only change its drawable. Is that possible?
Thank you in advance,
Gratzi
If I understand you correctly, you could set a click listener onto the ImageView that doesn't do anything. That way the ImageView will intercept the click that normally the view group gets.
If you use a TouchListener you can specify if the event is consumed or not. I use this code to press things within a GroupView, but the GroupView itself handles the OnClick
new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lefter.setPressed(true);
contents.setPressed(true);
}
else if (event.getAction() == MotionEvent.ACTION_UP || !isInside(v, event)) {
lefter.setPressed(false);
contents.setPressed(false);
}
return false;
}
//...
}
The returned boolean is documented as follows: "True if the listener has consumed the event, false otherwise. "