Java Android : onTouchListener release? - java

Java/android noob here, I have a problem where using onTouchListener I change the variable x to x+1 and that works fine, but when I release I would like the x variable to decrease in value until it hits zero, like so:
Imageview image = (ImageView) findViewById(R.id.fgView);
image.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
mode=1;
break;
case MotionEvent.ACTION_UP:
mode=2;
break;
case MotionEvent.ACTION_POINTER_UP:
mode2=2;
break;
case MotionEvent.ACTION_MOVE:
mode=1;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode2=1;
break;
}
}
if(mode==1){
x++;
}
if(mode==2){
x * 0.9
}
//mode2 is filler
}
Now that works fine, my problem comes when I want my x to continue to decrease after release, but since it's under setOnTouchListener then it doesn't work.
Maybe something like this:
public boolean onTouchFalse(View v){
if(x>0){
x * 0.9
}
}
or even something like
public boolean image.isOnScreen{
if(x>0){
x = x * 0.9
}
}
tl;dr: I'm looking for some kind of listener that I could use to depreciate my x.
Is there a listener or some class i'm missing? Thanks for any and all help!

final Handler handler = new Handler();
image.post( new Runnable() {
#Override
public void run() {
x--;
handler.postDelayed(this, 100);
}
});
** Note that the 100 is how long in between each subtraction (in millisecs)

There are no touch events if you're not touching the screen. Events need a trigger, maybe a timer could help.

Related

How do I get an image view to continually move across the screen when a button is held down?

I have searched high and low for an answer to this and can't find one anywhere that works.
For my university assignment i need to create an adventure game in android studio. I want it so when I click down and hold an arrow button (so in the case given its the up button), that the ImageView (player) will continually move across the screen until I release the button. Ive tried OnTouchListeners and mouse events using ACTION_UP and ACTION_DOWN and that works but not for what i need as it still only moves one step when clicked.
ImageView IV_player;
Button ButtonUp;
IV_player = (ImageView) findViewById(R.id.IV_player);
ButtonUp = (Button) findViewById(R.id.ButtonUp);
ButtonUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IV_player.setY(IV_player.getY() - 32);
}
});
Treat your touch listener like a state machine. When an ACTION_DOWN event occurs, start doing whatever action you want to do. When an ACTION_UP/ACTION_CANCEL event occurs stop your action. So how do you go about implementing this?
Your state flag can be a simple boolean:
boolean shouldCharacterMove = false;
Define your touch listener for the view.
ButtonUp.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
setShouldCharacterMove(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
setShouldCharacterMove(false);
break;
}
return true;
}
});
Before we define setShouldCharacterMove we need to figure out a way to move items. We can do this through a Runnable that runs after X milliseconds.
private final Runnable characterMoveRunnable = new Runnable() {
#Override
public void run() {
float y = IV_player.getTranslationY();
IV_player.setTranslationY(y + 5); // Doesn't have to be 5.
if (shouldCharacterMove) {
IV_player.postDelayed(this, 16); // 60fps
}
}
};
Now we can define setShouldCharacterMove:
void setShouldCharacterMove(boolean shouldMove) {
shouldCharacterMove = shouldMove;
IV_player.removeCallbacks(characterMoveRunnable);
if (shouldMove) {
IV_player.post(characterMoveRunnable);
}
}

Launch application on 2 finger swipe ANDROID

I have this code which doesn't return any errors. However it doesn't work like it's supposed to. I'm trying to launch an application when I swipe up with two fingers but nothing is happening. Can anyone see what the problem with my code?
public class HomeActivity extends Activity {
ImageButton phoneButton;
ImageButton contactsButton;
ImageButton messagesButton;
ImageButton cameraButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("Apps");
loadApps();
loadListView();
addClickListener();
addphoneButtonListener();
addmessagesButtonListener();
addcontactsButtonListener();
addcameraButtonListener();
//Two-Finger Drag Gesture detection
RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.mainLayout);
TextLoggerLayout.setOnTouchListener(
new RelativeLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
handleTouch(m);
return true;
}
});
}
void handleTouch(MotionEvent m){
//Number of touches
int pointerCount = m.getPointerCount();
if(pointerCount == 2){
int action = m.getActionMasked();
switch (action)
{
case MotionEvent.ACTION_DOWN:
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.googlequicksearchbox");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
}
}
else {
//do something
}
}
Try debugging handleTouch() to see exactly why its not firing. My guess would be because you have your check on MotionEvent.ACTION_DOWN, which in real world is a bit difficult to happen exactly at the same time for two fingers.
Move the check to MotionEvent.ACTION_MOVE (which is what you want anyway cause you want swipe, not tap) and try again.
MotionEvent.ACTION_DOWN means one finger. MotionEvent.ACTION_POINTER_DOWN means secondary touches (eg. two fingers).
So moving your code to case MotionEvent.ACTION_POINTER_DOWN: should solve your issue.
Documentation.

Double Tap in android View

In a view I want to detect both single tap and double tap. It works fine with me. I can detect both single and double tap. Please find my code
#Override
public boolean onTouch(View view, MotionEvent me) {
// No dragging during animation at the moment.
// TODO: Stop animation on touch event and return to drag mode.
boolean result = true;
result = gestureDetector.onTouchEvent(me);//return the double tap events
if(result)
return false;
switch (me.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
private String fileName;
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
// event when double tap occurs
#Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
if(player!=null && player.isPlaying())
{
player.stop();
}
else
{
setFileName(x);
player = new AudioPlayer(fileName);
player.play();
}
return true;
}
}
I am doing some code in Action_UP event. So even when I do a double tap, for the first tap the ACTION_UP event is called and the corresponding code is executed. I want to prevent this from happening. How can I do this. I just want to make sure that the ACTION_UP is called only when the user is done with his gesture. Not immediately after his partial gesture . In this case its being called on finger up of first tap.
How can I make this work in android?
Thanks
According to gesture listener documentation, you need to return true for onSingleTapConfirmed & onDoubleTap. Otherwise the event is not consumed and gestureDetector.onTouchEvent(me); returns false.

Lower continuously my media volume with a button

I want to lower (or upper) my media volume in my application when an "OnLongClickEvent" is detected.
Here my sources :
buttongauche.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
playSound(R.raw.volumevoixdiminue);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FLAG_SHOW_UI);
return true;
}
});
Actually, it's work : when I do a longClick on my "buttongauche", the volume is decreased by 1.
Now I would like to know how could I do if I want to lower the sound continuously (for example, decrease sound by 1 every 2 seconde when the button is down).
My button "buttongauche" has already an "onClickEvent", who do other things (change the index of a menu).
Thanks
Declare field boolean touching = false; that says whether or not you are touching the button and use OnTouchListener to change it. When you start touching also start volumeThread that lowers the volume every 1 second, and dies when you stop touching.
buttongauche.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touching = true;
Thread volumeThread = new Thread() {
public void run() {
while (touching) {
audio.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
volumeThread.start();
break;
case MotionEvent.ACTION_UP:
touching = false;
break;
}
return false;
}
});

issue with if/else and switch statements

the following code is to handle ACTION_DOWN and ACTION_UP events for a button named clash. the idea is that once if/else determines that the onTouch event was caused by clash and the switch statement then determines what to do based on the action. i don't know if the problem is that the switch statements are not returning true and that may be related to the problem. when i add a return, eclipse says that the code is unreachable which i don't understand. i was under the impression that you can't break out of a switch without break.
what's actually happening is that the first sound will loop but the code never seems to detect the action up when the button is released and so the sound plays forever. any help would be appreciated.
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
if (v.getId() == R.id.clash){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
}
return true;
}
});
//Add the following line in the code before your setOnTouchListener()
MediaPlayer mp;
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.clash){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
if(mp != null)
{
mp.pause();
mp.release();
}
break;
}
}
}
// I'm assuming this was from the onTouchListener()? -> });
Just a thought.

Categories

Resources