Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:47:17+00:00 2026-05-30T08:47:17+00:00

I have an activity with a layout contains an ImageView and TextView . ImageView

  • 0

I have an activity with a layout contains an ImageView and TextView. ImageView TextvVew are for showing the image and the name of the person respectively. I need to swap from right to left and the other way around with respect to users finger gesture. Same time, the name and the image must be changed accordingly.
Is there a way to implement this? It would be great if anyone would be able to let me know a tutorial written on this as I’m new to Android development.

Thank you.

I tried following ways but it does not detect any gesture.

MyGroupActivity.java

import goldenOld.pkg.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;

public class MyGroupActivity extends Activity implements OnGestureListener{

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.tab_mygroup);
}

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

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    Toast mToast2 =  Toast.makeText(getApplicationContext(), "Fling", Toast.LENGTH_SHORT);
    mToast2.show();
    try {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
        // right to left swipe
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
        }
        else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show();
        }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        // nothing
        Toast mToast =  Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT);
        mToast.show();
    }

    return true;

}

public void onLongPress(MotionEvent arg0) {
    Toast mToast =  Toast.makeText(getApplicationContext(), "Long Press", Toast.LENGTH_SHORT);
    mToast.show();
}

public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    return false;
}

public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent arg0) {
    Toast mToast =  Toast.makeText(getApplicationContext(), "Single Tap", Toast.LENGTH_SHORT);
    mToast.show();
    return true;
}

public class MyGestureDetector extends SimpleOnGestureListener {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Toast mToast =  Toast.makeText(getApplicationContext(), "Fling", Toast.LENGTH_SHORT);
        mToast.show();
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;

            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //do what you want on left swipe
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //do what you want on right swipe
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }

    public boolean onSingleTapUp(MotionEvent arg0) {
        Toast mToast =  Toast.makeText(getApplicationContext(), "Single Tap", Toast.LENGTH_SHORT);
        mToast.show();
        return true;
    }

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:47:18+00:00Added an answer on May 30, 2026 at 8:47 am

    You will have to use gesture detector for your purpose. See the following examples:

    • http://www.ceveni.com/2009/08/android-gestures-detection-sample-code.html
    • http://www.vogella.de/articles/AndroidGestures/article.html
    • http://www.hascode.com/2010/05/creating-a-simple-gesture-app-with-android/

    Also note that onFling() is the callback method which will be called when your activity detects any gesture. So here only you will have to write your logic of detecting which direction user is swiping.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a layout which contains some views like this: <LinearLayout> <TextView...> <TextView...> <ImageView
I have an activity whose layout only contains a VideoView. Here is the xml:
I have an Activity whose layout is in the file main.xml that contains a
I have a byte array which contains an image fetched from the net. I
i have an app widget, which contains only one imageview. i redraw that image
I have a relatively simple layout that contains an EditText . The activity itself
I have android app in which one layout contains Linearlayout , ScrollView , TextView
I have a FrameLayout that contains several ImageView. On the main activity, I record
I have an activity that contains several user editable items (an EditText field, RatingBar,
I have a main activity with this layout file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.