1

What I mean is, that I want to be able to press two separate buttons at the same time and have them do things on ACTION_DOWN and UP.

I have multiple onTouchListeners, but the app only lets me press one at a time. I need to be able to press at least two at the same time. I assume it has to do with the program getting stuck in a loop when the first button is being held down?

I read something about ACTION_POINTER1_DOWN and ACTION_POINTER2_DOWN, but I have to revise something to make them work right? Or am I even on the right track here?

package the1n07.bt_rc;

import android.content.Intent;
import android.gesture.Gesture;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;


public class ControlActivity extends ActionBarActivity {

    Button speedPlusButton, speedMinusButton, forwardButton, backwardButton, leftButton, rightButton;
    EditText speedText;

    public String speed;
    public Integer speedInt;
    public boolean forward = false, backward = false, left = false, right = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);

        Main();

    }

    public void Main(){
        speedPlusButton = (Button) findViewById(R.id.speedPlusButton);
        speedMinusButton = (Button) findViewById(R.id.speedMinusButton);
        speedText = (EditText) findViewById(R.id.speedText);
        forwardButton = (Button) findViewById(R.id.forwardButton);
        backwardButton = (Button) findViewById(R.id.backwardButton);
        leftButton = (Button) findViewById(R.id.leftButton);
        rightButton = (Button) findViewById(R.id.rightButton);



        speedPlusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speed = speedText.getText().toString();
                speedInt = Integer.parseInt(speed);

                if(speedInt != 5) {
                    speedInt = speedInt + 1;
                }
                speed = Integer.toString(speedInt);
                speedText.setText(speed);
            }
        });

        speedMinusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speed = speedText.getText().toString();
                speedInt = Integer.parseInt(speed);

                if(speedInt != 1) {
                    speedInt = speedInt - 1;
                }
                speed = Integer.toString(speedInt);
                speedText.setText(speed);
            }
        });





        forwardButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("forward", "is held down");
                        forward = true;
                        forwardButton.setBackgroundColor(0xFF7D7D7D);
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d("forward", "is lifted");
                        forward = false;
                        forwardButton.setBackgroundColor(0xff222222);
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        forwardButton.setBackgroundColor(0xff222222);
                        forward = false;
                        break;
                }

                return true;
            }
        });





        backwardButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            backward = true;
                            backwardButton.setBackgroundColor(0xFFB9B9B9);
                            break;
                        case MotionEvent.ACTION_UP:
                            backward = false;
                            backwardButton.setBackgroundColor(0xffffffff);
                            break;
                        case MotionEvent.ACTION_CANCEL:
                            backwardButton.setBackgroundColor(0xffffffff);
                            backward = false;
                            break;
                    }
                return true;
            }
        });





        leftButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                    switch (event.getAction()) {

                        case MotionEvent.ACTION_DOWN:
                            Log.d("left", "is held down");
                            left = true;
                            leftButton.setBackgroundColor(0xFF7D7D7D);
                            break;
                        case MotionEvent.ACTION_UP:
                            Log.d("left", "is lifted");
                            left = false;
                            leftButton.setBackgroundColor(0xff222222);
                            break;
                        case MotionEvent.ACTION_CANCEL:
                            leftButton.setBackgroundColor(0xff222222);
                            left = false;
                            break;
                    }

                return true;
            }
        });





        rightButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            right = true;
                            rightButton.setBackgroundColor(0xFFB9B9B9);
                            break;
                        case MotionEvent.ACTION_UP:
                            right = false;
                            rightButton.setBackgroundColor(0xffffffff);
                            break;
                        case MotionEvent.ACTION_CANCEL:
                            rightButton.setBackgroundColor(0xffffffff);
                            right = false;
                            break; //Does last case need break?
                    }
                return true;
            }
        });
    }

}
1N07
  • 123
  • 7

1 Answers1

1

This question may have already been answered by these two links : 1).Android - How to handle two finger touch. 2).Detect multitouch with Ontouch listener Android

The following links was also posted there : 1).http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html 2).http://developer.android.com/training/gestures/multi.html

Community
  • 1
  • 1
  • 1
    Thanks. I'll look into those links, but they seem(as do most others I've seen) to be about sort of "free" multitouch what I need is way to implement two buttoons so that I can press them at the same time. So I need a way to specify that when the ACTION_DOWN happens outside the button nothing happens. Is there a way to do this or do I have to know the coordinates of the button and use something like: if(ACTON_DOWN coordinates are within the buttons coordinates) {do something}? If so how do I get the coordinates of the button. – 1N07 Feb 20 '15 at 10:50
  • I think I actually found what I need: http://stackoverflow.com/questions/13722595/multitouch-issue-when-pressing-two-buttons-at-the-same-time-it-detects-as-if-i?rq=1 – 1N07 Feb 20 '15 at 10:54