I have an activity filled with buttons on the screen,and each has characteer text
something like : [A] [B] [C] etc. I need to code that when the user touches and slides his/her finger on the screen, it should aggregate the text of each touched button ,sample result should be : A + B + C = ABC . I have written some code but it does not work as intended . in my code ,I keep the array of buttons called buttons to identify which button is touched at that moment , but this code just work for first touch , it is Action_Down , it realize just first button which I touch , When I slide my finger around I cannot get other buttons texts. , So what is your suggestions ? help please . .
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
for(int i=0;i<buttons.size();i++){
if(buttons.elementAt(i).getId()==v.getId()){
text+=buttons.elementAt(i).getText();
break;
}
}
break;
case MotionEvent.ACTION_MOVE:
for(int i=0;i<buttons.size();i++){
if(buttons.elementAt(i).getId()==v.getId()){
text+=buttons.elementAt(i).getText();
break;
}
}
case MotionEvent.ACTION_UP:
Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
text="";
break;
}
return false;
}
I guess you think of multi touch, but your code is not.
For multi touch you need to use ACTION_POINTER_UP/DOWN. But not all devices support multi touch.
For non-multi-touch you need a more complex handling. Buttons may be selected. The join must be done explicitly.
Related
i'm writing an Android app for a school project that performs a different action depending on how many fingers the user taps the screen with.
Right now this action is just to display the number of pointers detected as a toast.
I'm using the getPointerCount() method, but for some reason, I get multiple toasts. For example, a three finger tap gives me toasts for two and three finger taps, a four finger tap will give me toasts for two, three and four finger taps, and so on.
I cannot for the life of me figure out why this is. A four finger tap should display ONE toast saying "4", not cycle through 2, 3 and 4! Any help would be greatly appreciated.
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch(action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_POINTER_DOWN:
int count = event.getPointerCount();
if (count == 2) {
finish();
}
else if (count == 4) {
Toast.makeText(this, String.valueOf(count), Toast.LENGTH_SHORT).show();
}
else if (count == 3) {
Toast.makeText(this, String.valueOf(count), Toast.LENGTH_SHORT).show();
}
break;
}
return true;
}
P.S, I have tried moving the code outside of the switch statement, and bizarrely , this causes the toasts to count up AND down!
You're thinking of the taps as discrete events, but Android MotionEvents are delivered as a stream. In order to get an ACTION_POINTER_DOWN with three fingers, it is required that you get an action pointer down with two fingers first. (If you think about it, all three fingers do not go down at the exact same time, even if this was possible you wouldn't receive them that way).
If you log every motion event with
Log.i("test", event.toString());
You should be able to see the sequence of events you are receiving, and better understand how to deal with them.
I have a problem with my app : I have a FrameLayout with a Fragment which contains a map from the Google Maps Android APi (v2), and a customized view which I use to display various buttons on top of the map.
I'd like some of these elements not to be shown while the map is being moved, for this I use the onTouchEvent() function of the View :
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
pressed = true;
invalidate();
break;
case MotionEvent.ACTION_UP:
pressed = false;
invalidate();
break;
}
return false;
}
The onDraw() function only displays the elements drawn over the map if pressed == false;
Now, my problem is in the return : if I put false, I say the event wasn't consumed, and the Android OS no longer notifies me when touch events are recorded, thus the MotionEvent.ACTION_UP isn't detected and the buttons stay hidden. If I put true, the touch events are no longer detected this time by the GoogleMap object, and I can't move the map around. Is there a way to keep detecting all future touch events, while not consuming the first event ?
Use onInterceptTouchEvent or dispatchTouchEvent.
You may read about the differences here: Android: Difference between onInterceptTouchEvent and dispatchTouchEvent?
So, here's my problem: in my game I made a countdown at the start of the game, during which the user can't press the screen, so I wait until the countdown is over to enable the OnSceneTouchListener. As soon as it's enabled, the user will be able to do some stuff moving the finger (not important what).
The problem is that if the user starts moving the finger while there's the countdown, as soon as it's over the onSceneTouchEvent method starts getting inputs, but it skips the ACTION_DOWN event since the user is already moving the finger.
To prevent this I could of course use a boolean inside the onSceneTouchEvent method, but this way it would always check that for every single input. It doesn't influence much the performance, but I'd rather find another way if it's possible.
So I was thinking, is there a way in the AndEngine to obtain the touch input event (with the information of the coordinate pressed, that's what I really need), so that I could "force" the ACTION_DOWN event by simple doing what I should do with it in the method that makes the countdown?
Thanks in advance!
Here is an example, how you can get touch coordinates.
public boolean onTouchEvent(MotionEvent event) {
int myEventAction = event.getAction();
float X = event.getX();
float Y = event.getY();
switch (myEventAction) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE: {
mySprite.setPosition(X, Y);
break;}
case MotionEvent.ACTION_UP:
break;
}
return true;
}
Source of this example
How to distinguish between Touch and drag in android... I want to create a listview in which people can drag and drop list items,that too with out using a handle...
What I have tried and failed is..
case MotionEvent.ACTION_DOWN:
mIsClickX = x;
mIsClickY = y;
and
case MotionEvent.ACTION_UP:
if(x == mIsClickX &&y == mIsClickY){
return super.onTouchEvent(ev);
}
Which doesn't work...
Thanks in advance for all your valuable suggestions
MotionEvent.ACTION_MOVE, you can also check:
1- http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2/1747
2- Android List View Drag and Drop sort
I think you can try this one.
A drag gesture starts when the first finger is pressed to the screen (ACTION_DOWN) and ends when it is removed (ACTION_UP or ACTION_POINTER_UP).
check this
I have to do the drag navigation in Android. I have added a code for the drag screen navigation. Which is not as smooth as Android default. The following code gives the drag navigation from left to right and right to left but the problem is when you tap at right and then to left then too the screen navigates. What is the right way to achieve it. Do I need to work with same code with calculating the X values?
Below the code.
// On Drag Navigation
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// Going Backward: pushing stuff to the right
if (downXValue < currentX)
{
Intent lIntent = new Intent(getApplicationContext(), previousScreen.class);
startActivity(lIntent);
finish();
}
// Going Forward: pushing stuff to the left
if (downXValue > currentX)
{
Intent lIntent = new Intent(getApplicationContext(), nextScreen.class);
startActivity(lIntent);
finish();
}
break;
}
}
}
Please specify the right way of achieving it.
Thanks in advance
You should be using a GestureListener
You probably don't want to use separate activities for this. This pattern is used to navigate within the same activity.
There are either two or three states to consider depending on how you choose to think about it. The first is the 'dragging' state when the user has a finger in the screen and is panning back and forth. The second and third are what happens when the user lets go. If the velocity when letting go is under a certain threshold, (the system uses the minimum fling velocity from ViewConfiguration) animate to the closest screen. If the velocity is over that threshold, fling to the next screen in that direction.
Take a look at the VelocityTracker, Scroller, and Interpolator classes. All of them can help you here.