I'm currently creating a custom double tap using the onClickListener with the following code:
newImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
counterTap++;
Timer t = new Timer("Double tap counter");
t.schedule(new TimerTask() {
#Override
public void run() {
counterTap = 0;
}
}, 0, 300);
if(counterTap >= 2) {
newImage.setVisibility(FrameLayout.GONE);
counterTap = 0;
}
}
});
The problem I'm facing is as follows:
Whenever I tap the ImageView the event does fire. However, the second time I tap the ImageView, the above code only executes when clicking on the exact same position on the ImageView as before.
Rather use a onTouchListener. In the touch listener in the onTouch method you can return false the first time you tapped which means the event was not handled, the second time you return true and the touch listener will handle the event as finished. See my example below, you can use a similar method
new OnTouchListener() {
boolean doubleTap = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!doubleTap) {
doubleTap = true;
return false;
} else {
return true;
}
}
}
};
this might solve your issue.
EDIT : This is a better opotion
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
// double tap event
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
}
This question might also help you get to the best answer
Related
I'm currently creating an app for SmartGlasses. It has an SDK that has some basic swipe Gesture and a custom Gesture Detector. I want to integrate this with my current code.
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (touches < 4) {
v.setBackgroundResource(picList.get(touches));
touches++;
} else {
touches = 0;
loop = 1;
v.setBackgroundResource(picList.get(touches));
}
}
});
I'm using this to go in a loop of images.
The SDK, one of its functions, is SlideRight(). And also a onTouchEvent().
#Override
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}else {
return false;
}
}
---------
#Override
public boolean onSlideRight(boolean b) {
return false;
}
How can I call this function and get it to do at least a Log.d when swiping right?
I am creating a video player in android studio. I want to hide buttons, layout and media controller after 5 seconds and I am using gestures for different properties but the issue is that when I apply 2 to 3 gestures in a certain time, after 5 seconds the buttons and media controller start blinking. I use this code for stying on screen
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.postDelayed(new Runnable() {
#Override
public void run() {
hide();
}
},5000);
}
}
return true;
}
});
Declare the runnable in global
Runnable mRunnable = new Runnable() {
#Override
public void run() {
hide();
}
};
call removeCallbacks before you call postDelay
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.removeCallbacks(mRunnable);//add this
handler.postDelayed(mRunnable, 5000);
}
}
return true;
}
});
I am trying to make a Helicopter-like game (as you can probably tell, I'm using "Rocket Man" instead of a helicopter). I'm new to Android programming and to Stack Overflow, so don't pick apart my code too much (please). Anyway, right now I'm just trying to get the rocketman to move up while touch is held down, and move down while touch is released.
It isn't working out very well.
I'm using a Moto XT886 for testing, (in case that's relevent). When I run the application, it starts up and loads the MainActivity perfectly well. When I touch the screen the first time (boolean start == true), the text views are hidden, as they are supposed to. However, on the second, third, fourth and all the rest of the times I touch the screen (Start == false), nothing happens. There are no errors in Log Cat, the code doesn't seem to recognize the touch.
I'm pretty sure that it has something to do with those while loops, but I'm using this application mainly to learn Android Java and I don't know enough (yet) to recognize that I'm doing something wrong. Are you able to put a while loop with the conditions as the MotionEvent of an onTouchListner? Or is it something else?
Either way, I appreciate the help, this is an awesome website (this is the first question I couldn't find already asked on Stack Overflow, and for all I know, mine could've already been answered)!
Here's the code to my MainActivity, if you need the .xml code as well, (though I'm pretty sure that's unnecessary) just say the word.
public class MainActivity extends ActionBarActivity {
boolean Start;
RelativeLayout rLayout;
TextView gameTitle;
TextView clickStart;
TextView highScore;
Handler handler;
ImageView rocketMan;
float Y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
rLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (Start == true) {
gameTitle.setVisibility(View.GONE);
clickStart.setVisibility(View.GONE);
highScore.setVisibility(View.GONE);
Start = false;
}
if (Start = false) {
while (event.getAction() == MotionEvent.ACTION_DOWN) {
Y = -7;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(100);
handler.post(new Runnable() {
#Override
public void run() {
HeliMove();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
while (event.getAction() != MotionEvent.ACTION_DOWN) {
Y = 7;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(100);
handler.post(new Runnable() {
#Override
public void run() {
HeliMove();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
return true;
}
});
}
private void initialize() {
rLayout = (RelativeLayout) findViewById(R.id.mainlayout);
Start = true;
gameTitle = (TextView) findViewById(R.id.rocketmanTitle);
clickStart = (TextView) findViewById(R.id.tapToStart);
highScore = (TextView) findViewById(R.id.highScore);
handler = new Handler();
rocketMan = (ImageView) findViewById(R.id.rocketMan);
}
private void HeliMove() {
rocketMan.setY(rocketMan.getY() + Y);
}
}
Thanks a million!
I have an activitiy where I have a button and when a click on this button I want to set a TextView with some value, so I used onClickListening and it is working:
ButtonPlus.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
ponts = ponts + 1;
resultadoTextView.setText(Integer.toString(ponts));
}
});
But the problem is that I want to keep increasing this textView's value while the button keep being pressed so I tried to use the OnTouchLister:
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
}
});
the problem is that when I give a fast click in the button it increments the TextView's value too much and I want the onTouchListener to be activated just after some time that the button was pressed.
any help please?
Use some additional counter.
int additionalCounter = 0;
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
++additionalCounter;
if (additionalCounter % X == 0) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
}
}
});
You can set X as you want, i.e. setting it to 5 would make touch event working 5 times slower.
Try this code.
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
ButtonPlus.setClickable(false);
//wait 1 second
ButtonPlus.postDelayed(new Runnable() {
#Override
public void run() {
ButtonPlus.setClickable(true);
}
}, 1000);
return false;
}
});
Im creating an OpenStreetMaps application using OSMDroid lib. I need LongPress gesture to add markers into an OverlayItem array to show them all, but i can't access to LongPress anyway.
I have tried with GestureListener, overridden the onTouch event, using GestureDetector(which is deprecated nowadays) and I can't find a solution for this problem.
The only way i have get the gesture is through setOnLongClickListener, but I have to choose between having longpress or panning the map, and I'm greedy and I want both :p
Have anyone any ideas?
Regards!
I have encounter a solution through OSMBonusPack library.
The object MapsEventsReceiver grants you a simple way to handle map's gestures.
You have to override singleTapUpHelper and longPressHelper methods and create a new overlay with this handler.
Here's the code:
MapEventsReceiver mReceive = new MapEventsReceiver() {
#Override
public boolean singleTapUpHelper(IGeoPoint arg0) {
Log.d("debug", "Single tap helper");
//your onSingleTap logic here
return false;
}
#Override
public boolean longPressHelper(IGeoPoint arg0) {
Log.d("debug", "LongPressHelper");
//your onLongPress logic here
setMarker(arg0);
return false;
}
};
//Creating a handle overlay to capture the gestures
MapEventsOverlay OverlayEventos = new MapEventsOverlay(getBaseContext(), mReceive);
map.getOverlays().add(OverlayEventos);
//Refreshing the map to draw the new overlay
map.invalidate();
//I use this method to set the marker on touchPoint
protected void setMarker(IGeoPoint arg0) {
touchPoint = (GeoPoint) arg0;
targetPoint = touchPoint;
path.addPoint(targetPoint);
//Listener to handle item's(markers) events
myOnItemGestureListener = new OnItemGestureListener<OverlayItem>() {
#Override
public boolean onItemLongPress(int arg0, OverlayItem arg1) {
Log.d("debug", "Testing long tap on item");
//your item onLongPress logic here
return false;
}
#Override
public boolean onItemSingleTapUp(int index, OverlayItem item) {
Log.d("debug", "Testing single tap on item");
//your item onSingleTap logic here
return true;
}
};
itemarray.add(new OverlayItem("Marker " + cnt, "Info about marker " + cnt, touchPoint));
ItemizedOverlayWithFocus<OverlayItem> overlayDeItems = new ItemizedOverlayWithFocus<OverlayItem>(getBaseContext(), itemarray, myOnItemGestureListener);
map.getOverlays().add(overlayDeItems);
overlayDeItems.setFocusItemsOnTap(true);
map.invalidate();
itemarray = new ArrayList<OverlayItem>();
cnt++;
}
private void setupGestureListener() {
GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
#Override
public void onLongPress(MotionEvent e) {
//do something
}
};
final GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener);
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
});
}