To check on which view I lifted the touch in onTouch - android

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;
}
});

Related

How to select GridView item and use onTouchListener in getView?(they seem to cancel each other out)

When i use an onTouchListener in the getView of my adapter the line
android:listSelector="#drawable/circle"
immediately stops working, if I set onTouch to return false it works again, however then the ACTION_DOWN ACTION_UP dosent work properly.
Heres what i have in onTouch
image.setOnTouchListener(new View.OnTouchListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Assets.playMusic(songID, false);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
Assets.mediaPlayer.stop();
Assets.mediaPlayer = null;
}
return true;
}
});
Its suppose to play music for as long as you have a finger held on the item and when you release it should stop the music. And it works well when returning true. However for some reason the circle stops appearing behind the tapped items. If it is set to false the circle appears, but then action_up dosent stop the music
ive tried using .setSelected .setActivated .setEnabled and none of them work
please help
Also i want it to work kinda like snapchats camera button, tap it and it does one thing, hold it and it does something for duration of your hold. I was going to use time variables in the Action up and down. but if anyone knows another way to do this id appreciate info about that too
In this situation is not encouraged to attach an OnTouchListener to the image, but to the items of the GridView instead.
You should have something like this:
GridView gridview = (GridView) findViewById(R.id.the_gridview_id);
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View v, int position, long id) {
doSomething();
}
});
EDIT:
In order to know how much time a view is pressed, you can do something like this:
// this goes somewhere in your class:
long lastDown;
long lastDuration;
...
// this goes wherever you setup your button listener:
gridview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
lastDown = System.currentTimeMillis();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
lastDuration = System.currentTimeMillis() - lastDown;
}
}
};

How can I know the view is in touch state in Android

How can I know the view is in touch state.
If more than one touch points on one view,How can I catch the event of the last up touch point.
Please help?
You can override onTouchEvent() on your View. ACTION_DOWN will be given when the first "pointer" is placed. From then on, you will get ACTION_POINTER_DOWN or ACTION_POINTER_UP as subsequent fingers are pressed down and then released. Then, when the last pointer/finger is released, your View will get ACTION_UP. This is spelled out clearly in the MotionEvent docs.
Something like this might be what you're looking for, just subclass whatever View you are working with.
#Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
isTouching = true;
else if(event.getAction() == MotionEvent.ACTION_UP)
isTouching = false;
return super.onTouchEvent(event);
}

Android - Button OnTouch event change focus of Button

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.

Issue in OnTouchListener for ImageView

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.

ImageView selector

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. "

Categories

Resources