0

I want to move the figure on list view item that contains phone number.

When I move figure from right to left, make a call. I'm failing to get that particoular item in list view to get the phone number and make a call.

My code is

        @Override
        arg1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent event) {

                float startX = 0;
                float startY;
                float endX;
                float endY;
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = (int) event.getX();
                    startY = (int) event.getY();
                    System.out.println("startX" + startX);
                    System.out.println("startY" + startY);
                    break;
                case MotionEvent.ACTION_MOVE:

                    endX = event.getX();
                    endY = (int) event.getY();
                    System.out.println("endX" + endX);
                    System.out.println("endY" + endY);
                    float sub = endX - startX;
                    System.out.println("sub" + sub);
                    if ((endX - startX) < 100) {
                        arg0.setBackgroundColor(Color.GREEN);

                    }
                    if ((endX - startX) > 100) {
                        try {
                            TextView ph_tv = (TextView) arg0
                                    .findViewById(R.id.textnum);
                            String Pho_no = ph_tv.getText().toString();
                            arg0.setBackgroundColor(Color.RED);
                            Intent intent = new Intent(
                                    Intent.ACTION_CALL, Uri
                                            .parse("tel:" + Pho_no));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            lv.setBackgroundColor(Color.BLACK);

                        } catch (ActivityNotFoundException e) {
                            Toast.makeText(
                                    getApplicationContext(),
                                    "Error in your phone call"
                                            + e.getMessage(),
                                    Toast.LENGTH_LONG).show();

                        }
                        section(path_name, lv1);

                    }
                    break;

                case MotionEvent.ACTION_UP:
                    endX = (int) event.getX();
                    endY = (int) event.getY();
                }
                return true;
            }

        });
888
  • 3,246
  • 8
  • 41
  • 60
Ravi Shinde
  • 17
  • 2
  • 10
  • b.w the question looks similar to [Android - basic gesture detection][1] [1]: http://stackoverflow.com/questions/937313/android-basic-gesture-detection – Rohit Feb 19 '13 at 13:27

1 Answers1

1
float hX = Float.NaN, historicY = Float.NaN;
static final TRIGGER_DELTA = 50; // Number of pixels to travel till trigger

@Override public boolean onTouchEvent(MotionEvent e) {

    switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
        hX = e.getX();
        historicY = e.getY();
        break;
    case MotionEvent.ACTION_UP:
        if (e.getX() - hX > -TRIGGER_DELTA) {
            onSlideComplete(Direction.LEFT);
            return true;
        }
        else if (e.getX() - hX > TRIGGER_DELTA)  {
            onSlideComplete(Direction.RIGHT);
            return true;
        } break;
    default:
        return super.onTouchEvent(e);
    }
}

enum Direction {
    LEFT, RIGHT;
}

interface OnSlideCompleteListener {
    void onSlideComplete(Direction dir);
}
Rohit
  • 6,941
  • 17
  • 58
  • 102