Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 550
Hisham Muneer Avatar asked Jul 06 '12 19:07

Hisham Muneer


2 Answers

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...

like image 111
D-32 Avatar answered Nov 07 '22 22:11

D-32


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

like image 35
Mightian Avatar answered Nov 07 '22 23:11

Mightian