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. "
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 have an activity with an expandableListView in it !
I would like to register to an event when someone clicks on the expandableListView, but not on a group nor an item. In other words, when there is space left under all items, i want to capture a click there.
I tried a lot of things, and the only thing that works for me is
expListView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(SetsActivity.this, "" + event.getY()), Toast.LENGTH_SHORT).show();
}
return false;
}
});
Unfortunately, this captures everything. I would like to be able to know if this is the empty space or not. Is there a way ?
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.
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.
Im working on an app with a friend and i want the button to visually press by changing the background and vibrating and then when released it does whatever that button is supposed to do.
the buttons i have right now only have an onclick method but i want it vibrate when touched and execute their function when clicked
i know of the ontouch and onclick methods but i cant seem to use them togetherand have already implemented both onclicklistener and ontouchlistener
how might i manage this.
You could do this by only using the OnTouchListener:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/* Change your button background and vibrate */
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
/* Button's functionality */
}
return true;
}
Hope it helps.