2

I'm trying this code to detect left and right swipes on scrollview.

Only leftToRight swipe gets detected, I'm not able to get RightToLeft swipe. Even scrolling scrollview it detects as leftToRight swipe.

I got this code from http://techin-android.blogspot.in/2011/11/swipe-event-in-android-scrollview.html

public class Programs extends AppCompatActivity implements View.OnTouchListener {

TextView title, desc;

static final int MIN_DISTANCE = 100;
private float downX;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.program);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    title = (TextView) findViewById(R.id.title_tv);
    desc = (TextView) findViewById(R.id.desc_tv);
    ScrollView s = (ScrollView) findViewById(R.id.content_scroll);
    s.setOnTouchListener(this);

    try {
      //setting title & desc
    } catch (Exception e) {
        e.printStackTrace();
    } 
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    try {
        getMenuInflater().inflate(R.menu.settingmenu, menu);
        if (menu != null)
            menu.findItem(R.id.share_menu).setVisible(true);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
        case R.id.settings:
            Intent setting = new Intent(Programs.this, Settings.class);
            startActivity(setting);
            break;
        case R.id.share_menu:
            try {
                Intent ourIntent = new Intent(Programs.this, Share_app.class);
                ourIntent.putExtra(
                        "send", "\n\n" + title.getText().toString()
                                + "\n\n" + desc.getText().toString());
                startActivity(ourIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case android.R.id.home:
            this.finish();
            break;
    }
    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
        }
        case MotionEvent.ACTION_UP: {
            float upX = event.getX();

            float deltaX = downX - upX;

            // swipe horizontal?
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                    this.onLeftToRightSwipe();
                    return true;
                }
                if (deltaX > 0) {
                    this.onRightToLeftSwipe();
                    return true;
                }
            } else {
                Log.d("swipes", "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
            }

        }
    }
    return false;
}

public final void onRightToLeftSwipe() {
    Log.d("swipes", "RightToLeftSwipe!");
}

public void onLeftToRightSwipe() {
    Log.d("swipes", "LeftToRightSwipe!");
}
}

any help? Thanks!

akshay bhange
  • 2,320
  • 2
  • 28
  • 46

0 Answers0