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 8120613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:12:15+00:00 2026-06-06T05:12:15+00:00

I have a RelativeLayout with a TextView in the middle. I’ve got it to

  • 0

I have a RelativeLayout with a TextView in the middle. I’ve got it to detect onFling, onDown, and onScroll events using SimpleOnGestureListener().

I would like the TextView to follow my finger around the screen (can be just in the x axis), and when I lift my finger for it so animate either out of the screen or back to the middle (depending on how far I’ve moved it).

  • 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-06-06T05:12:16+00:00Added an answer on June 6, 2026 at 5:12 am

    This is what I normally do in these cases.

    First of all, your onScroll method should look something like this:

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        // Make sure that mTextView is the text view you want to move around
    
        if (!(mTextView.getLayoutParams() instanceof MarginLayoutParams))
        {
            return false;
        }
    
        MarginLayoutParams marginLayoutParams = (MarginLayoutParams) mTextView.getLayoutParams();
    
        marginLayoutParams.leftMargin = (int) marginLayoutParams.leftMargin - distanceX;
        marginLayoutParams.topMargin = (int) marginLayoutParams.topMargin - distanceY;
    
        mTextView.requestLayout();
    
        return true;
    }
    

    We are modifying the leftMargin and topMargin an amount equivalent to the distance that has been scrolled.

    Next, to make the text view animate back to its original position you need to do so when the the event is ACTION_UP or ACTION_CANCEL:

    @Override
    public boolean onTouch(View arg0, MotionEvent event)
    {
        if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL)
        {
            snapBack();
        }
        return mScrollDetector.onTouchEvent(event);
    }
    

    Then in the snapBack method we animate back the text view:

    private void snapBack ()
    {
        if (mTextView.getLayoutParams() instanceof MarginLayoutParams)
        {
            final MarginLayoutParams marginLayoutParams = (MarginLayoutParams) mTextView.getLayoutParams();
    
            final int startValueX = marginLayoutParams.leftMargin;
            final int startValueY = marginLayoutParams.topMargin;
            final int endValueX = 0;
            final int endValueY = 0;
    
            mTextView.clearAnimation();
    
            Animation animation = new Animation()
            {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t)
                {
                    int leftMarginInterpolatedValue = (int) (startValueX + (endValueX - startValueX) * interpolatedTime);
                    marginLayoutParams.leftMargin = leftMarginInterpolatedValue;
    
                    int topMarginInterpolatedValue = (int) (startValueY + (endValueY - startValueY) * interpolatedTime);
                    marginLayoutParams.topMargin = topMarginInterpolatedValue;
    
                    mTextView.requestLayout();
                }
            };
            animation.setDuration(200);
            animation.setInterpolator(new DecelerateInterpolator());
            mTextView.startAnimation(animation);
        }
    }
    

    And that should do! You can modify the endValueX and endValueY variables to control where the text view goes back when you lift your finger.

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

Sidebar

Related Questions

I have an RelativeLayout and I would like to insert some textViews. Here is
I have a RelativeLayout with one child as <include another.xml> like this root.xml :
I have designed the screen using this relative layout. <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
I have a RelativeLayout with a few TextView as children <RelativeLayout android:id=@+id/shift_parent_name android:layout_width=fill_parent android:layout_weight=0.25
i have the following layout in xml <Relativelayout..... <TextView 1........../> <ImageView........./> <Textview 2......../> </Relativelayout>
I have RelativeLayout that consists of TextView s that I use to inflate items
I have a some ui widgets including textview inside RelativeLayout which is clickable. My
I have ArrayAdapter with this items structure: <?xml version=1.0 encoding=utf-8?> <RelativeLayout ... > <TextView
I have a problem with a RelativeLayout containing two TextView s and a ImageView
I have a form with a format of: <LinearLayout> <RelativeLayout>TextView EditText Button</RelativeLayout> <RelativeLayout>TextView EditText

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.