0

I'm making a fife application. My fife has 6 holes, that I consider them as 6 buttons. As you know, in fifes, it makes difference if you hold one, two or more holes (here buttons) at once. I'm new in android, how can I manage it? for example I want to do this:
if button1 and button2 are touched at same time : play sound1
if button6 and button2 are touched at same time : play sound2
if button1 and button2 and buuton3 are touched at same time : play sound3
.
.
.
finally i used this trick:

one.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            switch(event.getAction()){

            case MotionEvent.ACTION_DOWN:

                is1t = true;
                soundtoplay();
                one.setBackgroundResource(R.drawable.hintedholes);
                return true;

            case MotionEvent.ACTION_UP:

                is1t = false;
                soundtoplay();
                one.setBackgroundResource(R.drawable.holes);
                return true;

            }
            return false;
        }
    });

    two.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            switch(event.getAction()){

            case MotionEvent.ACTION_DOWN:

                is2t = true;
                soundtoplay();
                two.setBackgroundResource(R.drawable.hintedholes);
                return true;

            case MotionEvent.ACTION_UP:

                is2t = false;
                soundtoplay();
                two.setBackgroundResource(R.drawable.holes);
                return true;

            }
            return false;
        }
    });

    three.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            switch(event.getAction()){

            case MotionEvent.ACTION_DOWN:

                is3t = true;
                soundtoplay();
                three.setBackgroundResource(R.drawable.hintedholes);
                return true;

            case MotionEvent.ACTION_UP:

                is3t = false;
                soundtoplay();
                three.setBackgroundResource(R.drawable.holes);
                return true;

            }
            return false;
        }
    });
...

public void soundtoplay(){

    if(is1t == true && is2t == false && is3t == false && is4t == false && is5t == false && is6t == false){


        mp = MediaPlayer.create(Playing.this, R.raw.b);
        mp.start();

    }else if(is1t == true && is2t == true && is3t == false && is4t == false && is5t == false && is6t == false){

        mp = MediaPlayer.create(Playing.this, R.raw.a);
        mp.start();

    }else if(is1t == true && is2t == true && is3t == true && is4t == false && is5t == false && is6t == false){

        mp = MediaPlayer.create(Playing.this, R.raw.g1);
        mp.start();

    }else if ...

3 Answers3

1

In android 4.1+ (I guess), you can trigger multiple single touches without doing anything. But, to support lower versions, you can use ordinary multitouch handling methods, and handle different conditions. Check this out.

Serg Burlaka
  • 2,351
  • 24
  • 35
Kayhan Asghari
  • 2,817
  • 1
  • 28
  • 47
0

One approach is to set onTouchListener on your buttons instead of setting onClickListener. In this way you don't have to do an action every time a button clicked.
Then you have to handle simultaneous touches by measuring the time between the first click and the last click and conditioning on buttons that clicked in this interval.

SAbbasizadeh
  • 730
  • 10
  • 25
  • you mean doesnt matter how we touch them, we touch them with a delay after each other? how can i measure the time of clicks? for example i have to do sth like this: if button2 touched after 100 millisecond from button1 then i consider them touched simultaneous? – M.H.Rahnama Jul 14 '13 at 14:58
  • The delay between clicks depends on your implementation. You can get an idea on how to measure the time between your first click and last click by looking at this link: http://stackoverflow.com/questions/12762272/android-countdowntimer-additional-milliseconds-delay-between-ticks – SAbbasizadeh Jul 14 '13 at 17:01
-1

Please try to use bellow code.

public class MainActivity extends Activity implements OnTouchListener

{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button1).setOnTouchListener(this);
    findViewById(R.id.button2).setOnTouchListener(this);
    findViewById(R.id.button3).setOnTouchListener(this);
    findViewById(R.id.button4).setOnTouchListener(this);
    findViewById(R.id.button5).setOnTouchListener(this);
    findViewById(R.id.button6).setOnTouchListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onToch(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:
        Log.d("TAG", "button1 click");
        break;
    case R.id.button2:
        Log.d("TAG", "button2 click");
        break;
    case R.id.button3:
        Log.d("TAG", "button3 click");
        break;
    case R.id.button4:
        Log.d("TAG", "button4 click");
        break;
    case R.id.button5:
        Log.d("TAG", "button5 click");
        break;
    case R.id.button6:
        Log.d("TAG", "button6 click");
        break;

    default:
        break;
    }
}

}

nilesh patel
  • 834
  • 1
  • 5
  • 10