detect a continuous press on View - android

I need to play a certain sound as long as the user holds an ImageView pressed and pause it when the user stop holding it. Before doing anything with the MediaPlayer, I tried to test it with a Vibrator(please don't laugh at this point) object. I've implemented and set an OnTouchListener:
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
vib.vibrate(50);
break;
case MotionEvent.ACTION_MOVE:
/*--- no action required ---*/
break;
case MotionEvent.ACTION_UP:
vib.cancel();
break;
}
return false;
}
However, the vibrate action is only performed once, no matter how long I hold the ImageView pressed. I guess a MediaPlayer will react the same way. What am I doing wrong?
P.S. the press is detected correctly, because the Vibrator logs "cancel()" only after I release my finger.

It's stop because in function
vib.vibrate(50);
you set to 50 ms time in vibration check
void vibrate(long milliseconds)
Vibrate constantly for the specified period of time.
I think rest of your code is correct.

Related

How to play and stop sound by one button using Soundpool

I’m new one here and I’m kind of a new one in java. All my knowledge about code based on what I can find in web for my project. So in one hand I know some difficult stuff on the other hand I don’t know a simple things. Right now I’m stuck in this situation.
I have button with looped sound what I’m trying to do is when i touch button it should change image on "button_prees" when i release button it should play sound and change image on "button_on" So for that moment code below work fine but now I’m trying to do reverse sequence. You touch the same button and it change image on "button_prees2" than when you release button it should stop playing sound and change image on "button_off"
How implement this in code below ?
final Button b1 = (Button) findViewById(R.id.button1);
b1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
b1.setBackgroundResource(R.drawable.button_prees);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
myS.play(s1I, 1, 1, 1, -1, 1);
b1.setBackgroundResource(R.drawable.button_on);
break;
}
return true;
}
});
You could use a boolean as a flag for the Button state. Very briefly and basically:
boolean soundsOn = false;
public void setSoundsOn(boolean soundsOn){
this.soundsOn = soundsOn;
}
public boolean isSoundsOn(){
return soundsOn;
}
...
// Then for your Button listener:
onTouch(...)
if (!isSoundsOn){
// Your above above code to change resources, start playing
// EXCEPT
case MotionEvent.ACTION_UP:
...
setSoundsOn(true);
...
}else{
// Code to stop etc.
// INCLUDING
case MotionEvent.ACTION_UP:
...
setSoundsOn(false);
...
}
...
Ideally you should make sure sound has successfully started/stopped before calling setSoundsOn(...) otherwise your labels might end up saying what is not. Hope this helps...

Android - How to hold a button for some time to repeat an action?

I want to create a countdown timer in my app. There will be two buttons, one for increasing time and other for decreasing time. When the user holds down the button i want to get the app to repeat to add time.
I have already tried
this
but i got errors
You need to use onTouchListener
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
// You can call a thread here which will keep increasing the time
return true;
case MotionEvent.ACTION_UP:
// RELEASED
// Stop the thread here
return true;
}
return false;
}
});

Select other buttons on touch move

I'm writing a test which options are placed in buttons. I have a selector for buttons that displays normal and pressed state and everything works great.
What i want is that when user touches an option and moves to next option, the next button get focus and select. And when user takes the hand, perform the selected button click.
In images above you can see exactly what i mean.
You need to write a custom touch event management that will trigger setPressed(true) on MotionEvent.ACTION_DOWN OR MotionEvent.ACTION_MOVE (the default implementation expects to always receive MotionEvent.ACTION_DOWN).
Code like this should work (add it to all your buttons):
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (!v.isPressed()) {
v.setPressed(true);
}
return true;
case MotionEvent.ACTION_UP:
// perform action ON CLICK
break;
}
v.setPressed(false);
return false;
}
});

Android Listener to detect when the screen is being pressed

I have a view in my Android app that should be updated constantly when the screen is being pressed. Most listeners I found only invalidate the view once when the user touches the screen for the first time, or when the user removes his hand from the screen.
Is there such a thing as a listener that is constantly triggered as long as the screen is touched ? Or is there any other way to do this ? I know, that sounds like something really simple, but I haven't found a way to do it.
Override onTouch() and set a flag in ACTION_DOWN. As long as ACTION_UP isn't called after that, the user is touching the screen.
boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
pressed = true;
break;
case MotionEvent.ACTION_MOVE:
//User is moving around on the screen
break;
case MotionEvent.ACTION_UP:
pressed = false;
break;
}
return pressed;
}

Touching the screen while onTouch() is already being called

When I touch the screen and move my finger I do something (pullanimation1 and 2) and when I release the screen I do something else (fireanimation1 and 2). Sometimes, the user might touch the screen while pullAnimation or fireAnimation is running, I get errors as the animation then run several times. I would like to make sure the animations won't run more then once when the user touch again the screen.
NB: pullAnimation1 and 2, fireAnimation 1 and 2 are AnimationDrawable
Here is what I've done :
image2.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
boolean bool=false;
boolean bool2=true;
int action = arg1.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN:
if (bool2) {
startAnimation(pullAnimation1,pullAnimation2);
bool=true;
}
break;
case MotionEvent.ACTION_MOVE:
if (bool2==true){
Log.w("GAMEACTIVITY","move");
bool=true;
bool2=false;
}
break;
case MotionEvent.ACTION_UP:
startAnimation(fireAnimation1,fireAnimation2);
bool=false;
doPhotoTask();
bool2=false;
break;
}
return bool;
}
});
I think you should be able to use the hasStarted() and hasEnded() methods to determine if your animation is currently going. See the docs for more
Some if statement like this might work:
if((fireAnimation1.hasStarted() == false) || (fireAnimation1.hasEnded == true()){
startAnimation(fireAnimation1, fireAnimation2);
}
I imagine you may also need to use reset() after it is done playing in order or the methods to return proper values next time touch happens.
EDIT:
AnimationDrawable has an isRunning() method, which makes it even easier than View animations.
if(fireAnimation1.isRunning() == false){
startAnimation(fireAnimation1, fireAnimation2);
}

Categories

Resources