Hisham Muneer
Hisham Muneer

Reputation: 8742

how to move a button to left or right programmatically in android

I want to animate a button by getting its co-ordinates and then increasing or decreasing them one by one so that the button can go to left and then come to the right.

Upvotes: 7

Views: 10813

Answers (4)

santhosh pilot
santhosh pilot

Reputation: 11

When button clicked image moves from left to right you can use this code for fragments also.

insert this code in onCreateView for fragments or directly use button listener.

    View view = inflater.inflate(R.layout.fragment_move,container,false);
    mBtnMove = view.findViewById(R.id.btn_move);
    mImageMove = view.findViewById(R.id.imv_move);
    mBtnMove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            float start_x_axis = 0; // initialize the axis value start from and end
            float start_y_axis = 1000;
            float end_x_axis = 0;
            float end_y_axis = 0;
            TranslateAnimation animation = new TranslateAnimation(start_x_axis, start_y_axis, end_x_axis, end_y_axis);
            animation.setDuration(2500); // Duration of image to move from left to right
            mImageMove.startAnimation(animation);
        }
    });
  return view;

Upvotes: 1

Andrew Coder
Andrew Coder

Reputation: 246

   Solution for those who are looking for left to right animation)

            TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)

               animation.setDuration(1500); // animation duration
                animation.setRepeatCount(1); // animation repeat count
            animation.setFillAfter(false);
                your_view .startAnimation(animation);//your_view for mine is imageView     


Solution for those who are looking for repeated animation(for eg. left to right and right to left)

            TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
                animation.setDuration(1500); // animation duration
                animation.setRepeatCount(4); // animation repeat count
                animation.setRepeatMode(2); // repeat animation (left to right, right to left)

                animation.setFillAfter(true);
                your_view .startAnimation(animation);//your_view for mine is imageView 

Upvotes: 1

Aniruddha K.M
Aniruddha K.M

Reputation: 7511

Not sure if this will help you but i was struck with the same problem i was able to do this using these methods, setTranslationX(float) setTranslationY(float)

you can use it Like this

Button button = (button) findViewById(your id); 
button.setTranslationX(a float value);

here's the android documentation that provide more information http://developer.android.com/reference/android/view/View.html#attr_android:translationX

Upvotes: 4

D-32
D-32

Reputation: 3255

Use a TranslateAnimation:

TranslateAnimation animation = new TranslateAnimation(start_x, start_y, end_x, end_y);
animation.setDuration(1000); // duartion in ms
animation.setFillAfter(false);
button.startAnimation(animation);

I'm not sure how you can get it's position, button.getTop() and button.getLeft() could work...

Upvotes: 9

Related Questions