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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:18:00+00:00 2026-06-13T07:18:00+00:00

Need to make next animation (on android 2.2 and above): 1.Moving button from top

  • 0

Need to make next animation (on android 2.2 and above):

1.Moving button from top to bottom (after clicking on him),

2.Moving back from bottom to top (After clicking on him again).

First animation works fine, but the second not, the btn “jumps” from bottom to top and not animate.

Code:

public class MainActivity extends Activity {

static RelativeLayout relativeLayout;
static Button btn;
static Boolean isUp = true;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (Button) findViewById(R.id.button1);
    relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(isUp){
                isUp = false;
                v.startAnimation(MainActivity.getVerticalSlideAnimation(0,relativeLayout.getBottom() - v.getHeight(),500,0));
            }else{
                isUp = true;
                v.startAnimation(MainActivity.getVerticalSlideAnimation(relativeLayout.getBottom() - v.getHeight(),0,500,0));
            }
        }
    });
}


public static Animation getVerticalSlideAnimation(int fromYPosition, final int toYPosition, int duration, int startOffset)
{
  TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 0, fromYPosition, 0, toYPosition);
  translateAnimation.setDuration(duration);
  translateAnimation.setInterpolator(new AccelerateInterpolator());
  translateAnimation.setStartOffset(startOffset);

  //Stop animation after finishing.
  //translateAnimation.setFillAfter(true);

  translateAnimation.setAnimationListener(new AnimationListener() 
  {
    public void onAnimationStart(Animation animation) { }
    public void onAnimationRepeat(Animation animation) { }
    public void onAnimationEnd(Animation animation) {
        btn.setY(toYPosition);          
    }
  });

  return translateAnimation;
    }
}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

</RelativeLayout>
  • 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-13T07:18:01+00:00Added an answer on June 13, 2026 at 7:18 am

    Ok, I solved it.

    There are few issuses you should know about animation:

    1. The animation paremeters are not simple “From (fixed position)” –> “To (fix position)” as you should think. There are more like “From (current position/0)” –> “How much steps to do and on which direction (pluse for positive/ minus for negative)”

    2. The Animation doesn’t change the real position of the view on the screen, Therefore if you want to stop the animation at the end position, you should use:

      animation.setFillAfter(true);
      
    3. If you do want to change the REAL position of the view you should update the view parameters on “onAnimationEnd” (like below code), or calculate position and set Y/X position manually (again on “onAnimationEnd”), like:

      animatedView.setY(stopPosition);
      

    The Code:

        public class AnimationActivity extends Activity {
    
        private boolean isUp;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        ((Button) findViewById(R.id.button1))
                .setOnClickListener(new OnClickListener() {
    
                    public void onClick(final View v) {
    
                        final float direction = (isUp) ? -1 : 1;
                        final float yDelta = getScreenHeight() - (2 * v.getHeight());
                        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM;
    
                        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction);
    
                        animation.setDuration(500);
    
                        animation.setAnimationListener(new AnimationListener() {
    
                            public void onAnimationStart(Animation animation) {
                            }
    
                            public void onAnimationRepeat(Animation animation) {
                            }
    
                            public void onAnimationEnd(Animation animation) {
    
                                // fix flicking
                                // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker
                                TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
                                anim.setDuration(1);
                                v.startAnimation(anim);
    
    
                                //set new params
                                LayoutParams params = new LayoutParams(v.getLayoutParams());
                                params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                                params.addRule(layoutTopOrBottomRule);
                                v.setLayoutParams(params);
                            }
                        });
    
                        v.startAnimation(animation);
    
                        //reverse direction
                        isUp = !isUp;
                    }
                });
    }
    
    private float getScreenHeight() {
    
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return (float) displaymetrics.heightPixels;
    
    }
    

    }

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

Sidebar

Related Questions

I need to make button to start blinking after some other elemment trigered it,
I need to make the top header of a report disappear. The part that
Hai all i need to know how i can make next page in jQuery.
I need to make next task: file: string1 data1 data I need to create
I need to make an application that ,in my view there is a button
I need to make an effect similar to the Android effect when you slide
I need to make a mysql connection and get some data back. I can
I need to make an app, that it android based. What I need to
I need make some action (dump statistical data) before the Dart program ends. The
I need make all of my posts update. I use bulk upload for store,

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.