I want to write a program that handles touch events in a specific order within a single activity.
For example:
A few views are shown.
If the user doesn't tap on the first view, I will show another activity.
If the user taps on it however, I want to detect a tap on the second, then the third, etc.
How can I handle multiple touch events?
I think I need an onTouchEvent method and in it I need an if-else statement for the first click but I don't know how I can monitor for the subsequent touch events.
It may help you.I always do like this
public void onClick(View v){
if(v==imageView1){
//do ssomething
}
if(v== imageView2){
//do something
}
if(v==imageView3){
//do something
}
like this u can do according to different button or imageview
I'm assuming the picture is an ImageView inside the main view. Why not just append touchlisteners to each view?
Set the onClickListener for the n+1 view only when nth view is clicked.
Like this
view1.setOnClickListener(new OnClickListener() {
void onClick(View v) {
view2.setOnclickListner(new Onclicklistener() {
void onClick(View v) {
// add further view's click listeners else do what ever if this
// is the last view.
}
});
}
});
Not an elegant solution but should work IMHO.
From your problem statement it seems you have an ordered list of views, each should have a touch listener, but the listener for the second view should not fire unless the listener for the first view has fired first.
This can easily be done by keeping a counter in your activity:
private int highestIndexTapped. When a view is tapped, check whether its index is such that index == highestIndexTapped + 1.
If it is, increase highestIndexTapped by 1 and fire the listener. Otherwise either eat the touch event or pass it on to the next part of your pipeline.
Related
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;
}
});
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..
}
}
I have a view on which I want to add an onClickListener. Trouble is that the listener fires even when the user performs a drag operation. How do I suppress on click when the user drags, and only process when it is a click without dragging?
For completeness, am using something like below to add the onClickListener -
private void installClickHandler(final GraphicalView x) {
x.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), BillTablesActivity.class);
startActivity(myIntent);
}
}
});
}
set an onTouchListener(), which either returns true if you dragging, or returns false otherwise allowing your OnClickListener to handle the event. in other words, it's up to you to decide whether to treat the touch even as a click, or not.
to decide if something is a click or a drag (or a long press), the implementation of your OnTouchListener will have to keep track of ACTION_DOWN and ACTION_UP events, and compare the time between the two. a short interval would be a click, a longer time period would be a drag or a long press.
the specific implementation is left as an exercise for the reader :)
I have read some code that people use something like this
view.setOnLongClickListener(null);
What does it means and for what can be useful ? why someone uses this ?
is that the same as this
view.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
return true;
}
});
Null would remove any callbacks that are currently set as the views listener.
It definitely isn't the same as the second one, which assigns a listener to the view to control what will happen when you perform a long click on your view.
I want to use the same button to perform 2 different methods.
One method when user single clicks it and a second method (different) when the user LONG clicks it.
I use this for the single short click (which works great):
Button downSelected = (Button) findViewById(R.id.downSelected);
downSelected.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
method();
}
}
});
I've tried to add a longClickListener but it didn't work.
Appreciate any ideas on how to solve this.
Thanks!
I've done it before, I just used:
down.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Per documentation:
public void setOnLongClickListener
(View.OnLongClickListener l)
Since: API Level 1 Register a callback
to be invoked when this view is
clicked and held. If this view is not
long clickable, it becomes long
clickable.
Notice that it requires to return a boolean, this should work.
To get both functions working for a clickable image that will respond to both short and long clicks, I tried the following that seems to work perfectly:
image = (ImageView) findViewById(R.id.imageViewCompass);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
shortclick();
}
});
image.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
longclick();
return true;
}
});
//Then the functions that are called:
public void shortclick()
{
Toast.makeText(this, "Why did you do that? That hurts!!!", Toast.LENGTH_LONG).show();
}
public void longclick()
{
Toast.makeText(this, "Why did you do that? That REALLY hurts!!!", Toast.LENGTH_LONG).show();
}
It seems that the easy way of declaring the item in XML as clickable and then defining a function to call on the click only applies to short clicks - you must have a listener to differentiate between short and long clicks.
Initially when i implemented a longClick and a click to perform two separate events the problem i face was that when i had a longclick , the application also performed the action to be performed for a simple click . The solution i realized was to change the return type of the longClick to true which is normally false by default . Change it and it works perfectly .
Change return false; to return true; in longClickListener
You long click the button, if it returns true then it does the work. If it returns false then it does it's work and also calls the short click and then the onClick also works.
Try using an ontouch listener instead of a clicklistener.
http://developer.android.com/reference/android/view/View.OnTouchListener.html
The simplest and updated method is using a long click listener like
someView.setOnLongClickListener {
//do your work
true
}