Android Touch event with selected frame - android

I have a view animator that has a lot Button on it. I've a OnTouchEvent for the view . codes works fine if I touch on space, but if I touch the button the code doesn't work. how can i fix it?
final View frame = (View)findViewById(R.id.viewAnimator1);
frame.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
viewAnimator.showPrevious();
}
public void onSwipeLeft() {
viewAnimator.showNext();
}}

You need to set on click listener for buttons, if all of them makes the same work, you can extend button class and implement on click listener and use it instead of normal button

Related

Drag & Swipe in a RecyclerView List

I've been trying the ItemTouchHelper for Drag and Swipe actions in a RecyclerView List. I've used this guide and it's working perfectly, except that i can't make the swipe activate on LongClick. I've managed to do it for the Drag action:
textview.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
dragStartListener.onStartDrag(holder);
return false;
}
});
I don't know how to do it for the swipe, since it doesn't have an onStartSwipe() interface and any of my attempts to implement one, by mimicking the onStartDrag() interface, has failed.
How it can be made? Thank you for your time.
I've managed to implement the onStartSwipe() interface and it works by itself. However once i try to implement both drag and swipe on the same view, it acts strangely. More precisely, once i touch the view, for the entire hold down, only the drag or only the swipe animation would be activated.
Not only that, it is difficult to chose between the two actions since, it will get the first mirco movement you do to chose between either one or another.
I tried to this code for the above description:
tv.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mDragStartListener.onStartDrag(holder);
return false;
}
});
tv.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mSwipeStartListener.onStartSwipe(holder);
return true;
}
});
I also tried to use a framelayout like this:
tv.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mDragStartListener.onStartDrag(holder);
return true;
}
});
fl.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mSwipeStartListener.onStartSwipe(holder);
return true;
}
});
Here i drag the TextView and i swipe the FrameLayout. But since the text view is on top of the framelayout, it would only work dragging, and sometimes swipe if you touch on the corners of the item.
Any ideas on how to make both of drag and swipe work on the same item? I want to be able to either drag or swipe, once an element is LongClicked. I could use a separate ImageView for swiping and the TextView for dragging, but i'd rather not have to do that since it would ruin my design.
I am not sure why you are using draghelpers in recyclerview.
You can use ItemTouchHelper instead
It allows both drag and drop and swipe.
ItemTouchHelper
SimpleCallBack
Tutorial for implementation

Forward touch event to dynamically attached view, without lifting finger

I have a problem I am struggling with a while now.
I have a Layout with a Button and a container in it.
<FrameLayout ... >
<Button
android:id="#+id/button"
...
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/overlayContainer"/>
</FrameLayout>
My goal is that as I long-press the button, I attach a custom view MyCustomViewto the container and keep the finger pressed.
All the following (ACTION_MOVE, ACTION_UP) events should then ideally be dispatched to and evaluated by MyCustomView.
MyCustomView works like a circular flyout menu: it overlays, dims the background, and shows some options. You then slide your pressed finger to the option, lift it up, and it triggers a result.
mButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// attach custom view to overlayContainer
// simplified code for demonstration
overlayContainer.addView(new MyCustomView());
return true;
}
});
Right now I don't see any option to "steal" the ACTION_DOWN-Event (which is required to start the event flow to a view) from the Button as I'm above it.
Nor does it work to manually generate and dispatch a ACTION_DOWN-Event in MyCustomView as I attach it.
While researching I found this post here, it basically is the same requirement, but for iOS (also does not provide an elegant solution, other that an click capturing overlay view) ): How to preserve touch event after new view is added by long press
Note that I want to avoid some kind of global overlay over the main view, I would like the solution to be as pluggable and portable as possible.
Thanks for any suggestions.
To answer my own question after the hint in the comments:
I solved it using a bare stripped version of TouchDelegate (had to extend it, since it unfortunetaly is no interface - setTouchDelegate only accepts TouchDelegate (sub)classes. Not 100% clean, but works great.
public class CustomTouchDelegate extends TouchDelegate {
private View mDelegateView;
public CustomTouchDelegate(View delegateView) {
super(new Rect(), delegateView);
mDelegateView = delegateView;
}
public boolean onTouchEvent(MotionEvent event) {
return mDelegateView.dispatchTouchEvent(event);
}
}
Then in my onLongClick method:
mButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// attach custom view to overlayContainer, simplified for demonstration
MyCustomView myMenuView = new MyCustomView()
mButton.setTouchDelegate(new CustomTouchDelegate(myMenuView));
// What's left out here is to mButton.setTouchDelegate = null,
// as soon as the temporary Overlay View is removed
overlayContainer.addView(myMenuView);
return true;
}
});
This way, all my ACTION_MOVE events from the Button are delegated to MyCustomView (and may or may not need some translation of the coordinates) - et voilĂ .
Thanks to pskink for the hint.

Android AchartEngine: Panning and Click listener

My question is simple. But I am not getting. When panning then it also called the mChartView's OnClickListener. But I want when tap on graph then and then call OnClickListener
and when panning, disable the click.
I am giving my own question's answer. Just add the pan Listener to check the panning is continue or not. For that set a boolean and check condition in OnClickListener.
mChartView.addPanListener(new PanListener() {
#Override
public void panApplied() {
isPanEnables = true;
}
});

How other buttons disable when I press the button?

I have a few imageview which have onclicklistener. If I press one (not release), I can press click others or I can click them same time. I do not want it. Everytime when I press one of them others should be disable to click.
imageview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getMethod();
}
});
I guess, I tried setClickable(false); but it did not work properly, if I clicked one button after that it worked.
Try using onTouchListener instead of onClickListener and calling setEnabled(false); on the other views there. Here's a fairly basic example:
OnTouchListener onTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
imageView1.setEnabled(false);
imageView2.setEnabled(false);
}
return true;
}
};
And then apply it to the image views with:
imageView1.setOnTouchListener(onTouchListener);
That should work. One thing is, though, that while you'll only be able to push one button no matter what, you also won't be able to push anything after you let go - but, you can fix that by adding some logic to see if the view actually got clicked or if the user touched it, changed their mind and slid away. The (event.getAction() == MotionEvent.ACTION_DOWN) check will be true even if the user is just scrolling.
//button on which press u want to disable others
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2.setEnabled(false); //button which u want to disable
button3.setEnabled(false); //button which u want to disable
}
});
//update fixed a spelling error
try to disable the button and
button.setEnable(false);
enable the button
button1.setEnable(true);

How to disable onclick listener while swiping a view in android?

I have a view that can be swiped to another page. It has an onClick listener, which displays a dialog box.
The problem is, swiping triggers both actions (i.e. It shows the next page and the dialog box).
How can I disable the onClick listener when swiping.
It sounds like you want to use a GestureDetector (as well), the SimpleOnGestureListener has onSingleTapConfirmed() for click events and onFling() for swipe events.
While onClick of swipelayout put swipelayout.getSurfaceView() and while swiping it triggers only swipe action. See the code below:
holder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your onClick stuff here..
}
}

Categories

Resources