Touch events in Android - android

I have a button widget. I want to play sound when the button is pressed and when the user releases the button (takes his finger off the button) the audio playback should be stopped. I have used the following code but it doesn't work.
public boolean onTouch(View v, MotionEvent me) {
int action = me.getAction();
if(action == MotionEvent.ACTION_DOWN) {
playSound();
} else if (action == MotionEvent.ACTION_UP) {
stopSound();
}
return false;
}

Is stopSound() being called? I think you need to return true in the action == MotionEvent.ACTION_DOWN block to tell the system that you handled the event.

Related

Android OnTouchEvent not fire at first touch

OnTouchEvent is being fired after the second time touch; yet, I want it to be fired after the first touch itself.
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event) {
//too avoid touch being detected continuously
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (Function.system[5] == "Gravity: on") {
//some action...
}
}
return super.onTouchEvent(event)
}
You need to tell Android to continue to respond to touch events. Returning true let's you capture events from action down , action move and then action up.
Please add the return true for each event
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event) {
//too avoid touch being detected continuously
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
return true;
}
Please try and seee if this works for you

Detect ongoing touch event

I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}

Android call event continously when press button

I have to develop one apps, in that i want to fire one action, like button is vibrate contentiously during press,i leave press it stop vibration.
In onTouch - event occur only during press,but when i press button contentiously -> event occur contentiously
like i press button for 1 minute it vibrate for same time,if leave press it stop.
i don't know which method used for this, so, please any one help me to do like this.
my code is below to do that : but it not work for continuous action during press (using thread it work for vibrate only, if i join more code with that it give error see in following.
**Edit : **
i got solution for vibrate using like below but when i write code (below vibrator.vibrate(100); ) for some animation that continuous during press then i got error : Only the original thread that created a view hierarchy can touch its views. i also try with runOnUIThread also but with that it not work.
img_laser_ballpen.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MainActivity.this.vibrating = true;
img_laser_light.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
public void run() {
while (MainActivity.this.vibrating) {
// ADD CODE FOR MAKING VIBRATION
vibrator.vibrate(100); // it works properly
//Animation shake = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.shake);
//img_laser_light.startAnimation(shake); //if it open then give error
}
//
}
}).start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
MainActivity.this.vibrating = false;
img_laser_light.setVisibility(View.INVISIBLE);
}
return vibrating;
}
});
You could like this
Button btn = (Button) findViewById(YOUR_BUTTON_ID);
btn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event){
if(event.getAction() == MotionEvent.DOWN){
// Start vibrating
}else if (event.getAction() == MotionEvent.UP){
// Stop vibrating
}
}
});
This answer has a good example of catching these events; namely - you should use the OnTouchListener instead of OnClickListener.
// this goes wherever you setup your button listener:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// START VIBRATE HERE
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// STOP VIBRATE HERE
return true;
}
}
};
Edit:
You should return true after handling your vibrate so that other events are fired for this view. From the documentation:
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
You could opt for the following:
Button btn = (Button) findViewById(YOUR_BUTTON_ID);
boolean vibrating = true;
btn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View view, MotionEvent event){
if(event.getAction() == MotionEvent.DOWN){
YOUR_CLASS_NAME.this.vibrating = true;
new Thread(
new Runnable(){
public void run(){
while(YOUR_CLASS_NAME.this.vibrating){
//ADD CODE FOR MAKING VIBRATION
}
}
}
).start();
}else if (event.getAction() == MotionEvent.UP){
YOUR_CLASS_NAME.this.vibrating = false;
}
}
});

OnTouch with multitouch in android

I am havig troubles counting how much touches are being made in each "half" of the screen.
When I touch in the upper side it counts correctly, and the same with the lower side. BUT, when I touch the upper side, dont release it, and touch the lower side, then it counts the wrong side, what is wrong in my implementation?
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) {
if(event.getY()<400)
{
}
else
{
}
}
if (action == MotionEvent.ACTION_UP) {
if(event.getY()<400)
{
zenbat=zenbat+1;
tv.setText(String.valueOf(zenbat));
}
if(event.getY()>400)
{
zenbat1=zenbat1+1;
tv1.setText(String.valueOf(zenbat1));
}
}
if (action == MotionEvent.ACTION_POINTER_DOWN) {
if(event.getY()<400)
{
}
else
{
}
}
if (action == MotionEvent.ACTION_POINTER_UP) {
if(event.getY()<400)
{
zenbat=zenbat+1;
tv.setText(String.valueOf(zenbat));
}
if(event.getY()>400)
{
zenbat1=zenbat1+1;
tv1.setText(String.valueOf(zenbat1));
}
}
return true;
}
});
If you want to correctly handle multitouch events you need to use the pointer index to correctly identify the finger generating the event.
I've answered a similar question in Android multi-touch interference where I posted a code example how to do it.
To correctly identify the finger, you should refer to the fingerId in the code posted.
Regards.

onTouch on ListView, change color of items

I have a custom listview in my android application. When a user press an item in the list I want to change the background color of the pressed item. This is the code for that behaviour:
tempView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isDown = false;
tempView.setBackgroundColor(Color.parseColor("#f47920"));
}
if (event.getAction() == MotionEvent.ACTION_UP) {
tempView.setBackgroundResource(R.drawable.list_selector_focused);
}
if(event.getAction() == MotionEvent.ACTION_MOVE) {
tempView.setBackgroundResource(R.drawable.list_selector_focused);
}
return false;
}
});
But when I "Fling" my finger over the screen to scroll the listview, an item is also marked, and the "pressed" color will be static. How can I avoid this?
MotionEvent.ACTION_UP will not be caught unless you return true from onTouch() to inform Android system that you handled the whole touch event.

Categories

Resources