tags:

views:

16

answers:

1

I would like to change margin of my buttons in d applcn... suppose I have 5 buttons like this in a linear layout, one below d other...

When I focus on a button, its width should increase (which I know how to handle) and at the same time, width should increase linearly towards both left and right.

i.e. If the current width of the button is 250 with x co-ordinate as 50 (that means button's left is 50 and button's right is 300), when I focus on the button I should get x co-ordinate as 75 (left = 25 and right = 325). How to change the margin/x co-ordinate of the button?

A: 

Check out the Animation and ScaleAnimation classes at http://developer.android.com/reference/android/view/animation/Animation.html and http://developer.android.com/reference/android/view/animation/ScaleAnimation.html.

One way to do it is to define two XML files in your res/anim directory, one to make the button larger, and the other to make it smaller. So for makeLarger.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
android:fromXScale="1" 
android:toXScale="1.1" 
android:pivotX="50%" 
android:pivotY="50%" 
android:duration="200" 
android:fillAfter="true" />

Here, you're increasing the width by 10%, from 1.0 to 1.1. For the makeSmaller.xml file, swap the values of android:fromXScale and android:toXScale, or just set them as you like. To apply the animation to your button,

Button myButton = (Button)findViewById(R.id.myButton);
myButton.setAnimation(AnimationUtils.loadAnimation(this, R.anim.makeLarger));

Of course, the makeLarger and makeSmaller animations should be set when you touch the button.

I hope this helps!

cfei
of cos...it did help..thanx a lot... :-)
mdv