0

So I have a basic linear layout that consists of 6 ImageButton elements that stretch over the whole screen in a 2x3 grid. I want to make an app that works as follows:

*1)*if any of the ImageButtons is short clicked, it will start another activity

*2)*if user holds his finger on the screen, it plays a sound according to on which of the ImageButtons the finger is (so 6 different sounds)...if he moves his finger(still touching the screen) to another ImageButton, it will play another sound (all without lifting his finger)...and finally when the finger is lifted up, it will start the activity according to the element, where the finger was lifted.

My question is, what approach to choose to do this? MotionEvent class? Because just onClick() of OnClickListener won't be enough IMHO. Thanks

turor
  • 1
  • 1

1 Answers1

0

I think you are expecting two different functions for Same image But you can achieve this concept by using the Click and Long press You cannot achieve with single touch I will suggest you to use this long press and single Click Example for this problem is.....I will explain for the single button you will repeat it for Other Five buttons....

 but.setOnTouchListener(new OnTouchListener() {         
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            detector = new GestureDetector(getApplicationContext(), new TouchDetector());

            return false;
        }
    });


 public class TouchDetector implements android.view.GestureDetector.OnGestureListener   {

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }



}

You try this for Achieve your Logic But it's a simple Logic you can use this Logic also

public class MainActivity extends Activity {
GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button but1 = (Button) findViewById(R.id.button1);
    Button but2 = (Button) findViewById(R.id.button2);
    Button but3 = (Button) findViewById(R.id.button3);
    Button but4 = (Button) findViewById(R.id.button4);
    Button but5 = (Button) findViewById(R.id.button5);
    Button but6 = (Button) findViewById(R.id.button6);
    but1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Do here your Activity calling functions....
        }
    });

    but.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            //Do your sound Logic here
            return false;
        }
    });
// you just repeat this for all other Five buttons...        
}
}
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12
  • First, thank you for your resposne. I think I asked something different than I wanted. I want this: If a user first clicks an ImageButton, it should make a sound. But then, if he doesn't release his finger and continues somewhere else and then he releases his finger on different ImageButton, it should do an event connected to the last ImageButton, not to the first clicked one. I am trying to accomplish this by using EventMotion.ACTION_DOWN and EventMotion.ACTION_UP but the ACTION_UP is always connected to the first ImageButton, where ACTION_DOWN was performed. Any help? Thanks! – turor Mar 23 '14 at 12:39