Move finger across buttons in android apps - android

When the user touches the button sound shoud be played. There is a lot of buttons, and each has a different sound. When user move finger across all buttons, all sounds should be played. Something like piano app. How can I do it? I tried with ontouch lister but it doesn't work.

Within your onTouch method, you should capture different events starting from user TouchDown followed by TouchMove and finally TouchUp. Look for all x,y co-ordinates whether in your button area, if so, play sound.
Make sure that the current button selection is different than the previous one otherwise, if your finger moves over same button, that'll trigger another event for same button and you'll here tons of sound while moving finger:
psudo code:
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_UP: // stop here
break;
case MotionEvent.ACTION_DOWN: // start here
break;
case MotionEvent.ACTION_MOVE:
// See if new x y co-ordinates in your buttonRect area
// RectF [] buttonRect = new buttonRect[10] ;
for(int i = 0; i < 10; i++)
{
if( buttonRect[i].contains(event.getX(), event.getY()))
{
// if it's a new button found than previously touch, play a sound
// store the button number that's been tapped by user
}
}
break;
}
}
Hope it helps.

Related

Touch event working but not expected output

alpha_image.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action=event.getAction();
// TODO Auto-generated method stub
switch (action)
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
next();
break;
}
case MotionEvent.ACTION_UP:
{
// store the X value when the user's finger was pressed down
previous();
break;
}
}
return false;
}
});
This is the code I used for image move by finger touch but it is working oneside
(only moving left side) can someone please help me to move image right side also ,that is based on the user touch the image need to move.
Thanks in advance.
I suspect that you misunderstood the concepts of ACTION_UP and ACTION_DOWN (or you just trying to create some very customized UI) but nevertheless, you should return true after intercepting ACTION_DOWN in order ACTION_UP to get fired when you lift your finger.

Interesting android touch issue

The problem that i am having is I am using a touch listener for the onTouch event it seems that the touch is being called more than once when i just touch it once.
The following is my code
final TextView tv = (TextView)findViewById(R.id.TextView01);
tv.setOnTouchListener(new View.OnTouchListener() {
int count1 =0;
int count2=0;
public boolean onTouch(View arg0, MotionEvent event) {
Log.d("Code777","Touch is being called finger");
int i = event.getPointerCount();
if(i==1 )
{
if(count1==0)
{
Log.d("Code777","Touched with 1 finger");
count1++;
count2=0;
}
}
if (i==2 )
{
if(count2==0)
{
Log.d("Code777","Touched with 2 fingers");
edit.append(tv.getText());
count2++;
count1=0;
}
}
return true;
}
});
Am i doing something wrong ??
It prints the log more than 3-4 times for both single touch and double touch
The problem updated problem is that both the events are getting fired now
if(event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN)
{
Log.d("Code777","2 finger touch");
return true;
}else if(event.getAction() == MotionEvent.ACTION_DOWN)
{
Log.d("Code777","Touched with 1 finger");
return true;
}
You're code will execute during every touch event. The first time it activates, it's likely do to an ACITON_DOWN event (when the user first touches the screen). The second time, it is likely do to an ACTION_UP event (when the user lifts the finger from the screen). Likewise, if you were to swipe your finger around the screen, the same code will execute many times for an ACTION_MOVE event. You have to check for the types of touches that it is. Do something like this:
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// Do something that should only happen when the user first touches the screen
break;
case MotionEvent.ACTION_MOVE:
// Do something that should only happen when the user is touching the screen
break;
case MotionEvent.ACTION_UP:
// Do something that should only happen when the user stops touching the screen
break;
}
EDIT:
Multitouch in Android is odd at best. Not all devices can handle it. Not all device can handle more than x number of touches, etc. If you want to handle DOWN cases for individual fingers, then you can use the ACTION_POINTER_X items. If you were to have this series of events like so:
1. User touches screen
2. User touches screen with second finger
3. User lifts finger 1
4. User touches screen with finger 1
5. User lifts finger 2
6. User touches screen with finger 2
7. User lifts both finger 1 and finger 2
8. User touches screen with only finger 2
9. User touches screen with finger 1
The events that will be fired will be like this:
1. ACTION_DOWN
2. ACTION_POINTER_2_DOWN
3. ACTION_POINTER_1_UP
4. ACTION_POINTER_1_DOWN
5. ACTION_POINTER_2_UP
6. ACTION_POINTER_2_DOWN
7. ACTION_UP (also, one of the pointers actions will fire depending on which finger was lifted first)
8. ACTION_DOWN
9. ACTION_POINTER_2_DOWN
And so on.
An onTouchListener sends you multiple actions via MotionEvents. You can get the action for the current event with MotionEvent.getAction().
What you notice here is most likely that you get one ACTION_DOWN (a finger has been placed on the display) event followed by some small ACTION_MOVE events when you move the finger(s) slightly. In the end you will get ACTION_UP (a finger has been lifted) You can filter these out by ignoring events with the wrong action.
For example just return from onTouch() when a move event occured.
public boolean onTouch(View arg0, MotionEvent event) {
Log.d("Code777","Touch is being called finger");
if(event.getAction() == MotionEvent.ACTION_MOVE) {
Log.d("Code777", "Move event detected, ignored!");
return false;
}
// Further processing ..
}
or use a switch to differentiate between all relevant events.
See the MotionEvent class documentation for a list of possible actions.
.

"Live" get(x) and get(Y) values (for MotionEvents) ****UPDATE****

I was wondering how to get accurate, live get(x) and get(y) values for a MotionEvent? What is happening is that when I touch a specific area on the screen, I tell an action to happen.
The problem is that once I touch the screen and take my finger off, it still thinks my finger is at the same location (since that was the last location I touched). So when I have more than one Down event (for multitouch) it throws everything off. Is there a way to reset the X and Y values so when I let off the screen, they go back to 0 or null (or whatever)? Thanks
What you are describing isn't a problem. You the programmer are responsible for keeping track of the touch locations and what they mean. If you care about motion you need to keep track of the previous touch and the current touch each time a touch occurs. I find something like this works great:
public int x=-1,y=-1,prevX=-1, prevY=-1;
public boolean onTouch(View v, MotionEvent event)
{
prevX = x;
prevY = y;
int x = (int)event.getX();
int y = (int)event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// there is no prev touch
prevX = -1;
prevY = -1;
// touch down code
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
Just catch the MotionEvent.ACTION_UP or ACTION_MOVE event, depending on when the action needs to happen (since you want it live, you should use the ACTION_MOVE event). You should handle setting external variables when ACTION_DOWN occurs, and handle the results on ACTION_MOVE or ACTION_UP.

How to handle touch up event in GridView's child views

Application have a GridView with a images. When user press image - looped sound start playing. When user releases image - sound must stop playing.
I cannot find the way to handle release event.
write touch event in getview method of adapter class. it will work.
public boolean onTouch(View arg0, MotionEvent e) {
switch(e.getAction()){
case(MotionEvent.ACTION_DOWN):
//the user put his finger down on screen
break;
case(MotionEvent.ACTION_MOVE):
//the user is moving with his finger down
break;
case(MotionEvent.ACTION_UP):
//the users finger is off the screen
break;
}
}

How to set finger touch functionality in android?

I am developing a small application in android in which i have few image in my application i want when user touch with one finger images can move left or right side and when user can touch with two fingers it could be zoom how can i do this please refer some tutorial code for me.
here is my code
and i used view flipper in xml v fdjf
public class Jaap extends Activity implements OnTouchListener{
float downXValue;
int counter = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set main.XML as the layout for this Activity
setContentView(R.layout.jaap);
// Add these two lines
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
// Add a few countries to the spinner
}
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 backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 131){
vf.showNext();
counter++;
}
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}
}
Check out this step-by-step tutorial, it contains code examples how to zoom in/out a picture and move it around.
Handle the MotionEvent in the onTouchEvent(MotionEvent) of the Activity.
In this check for the MotionEvent.getAction().
switch(MotionEvent.GetAction()) {
case ACTION_DOWN:
//handle the finger down functionality here
break;
case ACTION_POINTER_DOWN:
//handle the second finger down functionality here
break;
}
A sequence of events would be issued, mostly with the actions as follows:
ACTION_DOWN - one finger touch down
ACTION_MOVE -> the finger is moved
ACTION_UP - one finger touch is removed
ACTION_POINTER_DOWN - second finger touch down
ACTION_POINTER_UP - second finger touch up
You will have to check the X,Y positions in the event and determine what should be donw...
will see if there are any good tutorials/samples to explain these better...
getPointerCount() of motionEvent tells you how many fingers are touch. In gives number of touch
Thanks

Categories

Resources