Android - Change left margin using animation

android property animation
android objectanimator scale
move animation in android
android animate layout changes programmatically
translate animation android
android path animation example
android-switch animation not working
android animating a view

I am changing the left margin of an image view in the following manner :

ViewGroup.MarginLayoutParams layoutParams = (MarginLayoutParams) image.getLayoutParams ();
layoutParams.leftMargin = VALUE;
image.setLayoutParams ( layoutParams );

I would like the change in margin to apply with animation. Any clues ?

What I tried :

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat ( image , "x" , VALUE);

This works perfectly, as the image is moved to the specified X value with animation, HOWEVER the value of layoutParams.leftMargin remains unchanged !! So I cannot use this method, because if I try to change the value of layoutParams.leftMargin to 100 after using the objectAnimator with the value 100, the value applied is not correct ( 200 is applied instead of 100, the effect if the objectAnimator remains eventhough I am setting the left margin in the following manner :

layoutParams.leftMargin = 100;

Use Animation class, not ObjectAnimator.

final int newLeftMargin = <some value>;
Animation a = new Animation() {

    protected void applyTransformation(float interpolatedTime, Transformation t) {
        LayoutParams params = yourView.getLayoutParams();
        params.leftMargin = (int)(newLeftMargin * interpolatedTime);
a.setDuration(500); // in ms

Please note that you should use correct LayoutParams class i.e. if your view is the child of LinearLayout then params should be LinearLayout.LayoutParams

Animate margin change, I use this code for animate view in android its work perfect problem when I set margin to zero or The problem is that you didn't use the ValueAnimator.ofInt(int.​.. values); correctly: you should explicitly tell the animator from which to which value it should animate. ofInt(50, margin); leftMargin(100). Animations, when used correctly, can be a simple way to enhance the user experience of your products, adding a little bit of fun that a motionless view just doesn't have. Today I'll be demonstrating how to add some basic left and right sliding animations to your Views and Activities on Android.

I came by this question, but I couldn't use it because I want to animate the margin from a negative value to 0, so I used valueAnimater base on user1991679 answer:

final View animatedView = view.findViewById(;
final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) animatedView.getLayoutParams();
ValueAnimator animator = ValueAnimator.ofInt(params.bottomMargin, 0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    public void onAnimationUpdate(ValueAnimator valueAnimator)
        params.bottomMargin = (Integer) valueAnimator.getAnimatedValue();

You must change LinearLayout.LayoutParams according to animatedView container. Also you can use nineoldandroids for older version that don't have ValueAnimator.

Change left margin using animation - android, getLayoutParams (); layoutParams.leftMargin = VALUE; image.setLayoutParams ( layoutParams ); I would like the change in margin to apply with animation. In this tutorial we will be using the following: – Android studio version 3.0.1 – Android emulator Nexus 5X with API 26 – Minimum SDK API 16 1- Open up Android Studio and open any project that you have in your computer. Create new Android Studio project or open existing project. (Large preview)

The answer from user1991679 is great, but if you need to interpolate a margin from any other value but 0, you need to use it in your calculations:

ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mBottomLayout.getLayoutParams();
final int bottomMarginStart = params.bottomMargin; // your start value
final int bottomMarginEnd = <your value>; // where to animate to
Animation a = new Animation() {
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mBottomLayout.getLayoutParams();
        // interpolate the proper value
        params.bottomMargin = bottomMarginStart + (int) ((bottomMarginEnd - bottomMarginStart) * interpolatedTime);

In my case I needed to animate an "enter the screen" animation, coming from "-48dp" to 0. Without the start value, the animation is always 0, thus jumping, not animating the view. The solution was to interpolate the offset and add it to the original value.

Android - Change left margin using animation, leftMargin = VALUE; image.setLayoutParams ( layoutParams );. I would like the change in margin to apply with animation. Any clues ? What I tried  Android offers pre-loaded animation that the system runs each time you make a change to the layout. All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you. Tip: If you want to supply custom layout animations, create a LayoutTransition

You can use the following


You can also add .setInterpolator(new BounceInterpolator()) to change the look of the animation.

Property Animation Overview, Use a wrapper class that you have rights to change and have that wrapper receive the value with a valid setter method and forward it to the  Therefore, to animate it to the left/right, simply change the css: $(".MY_CLASS").css("margin-left", "-300px"); Note this will animate it to a fixed position of 300px, if you want to animate to a position relative to its current location use:

[android] Animate margins with ObjectAnimator · GitHub, import android.view.ViewGroup.MarginLayoutParams;. /**. * Allows an ObjectAnimator to set/get margins of a view. */. class MarginProxy {. Animating marginLeft with jQuery. Ask Question I was using CSS3 for animation, but need to make it work in older browsers. jQuery margin-left not working-2.

Advanced Android in Kotlin 03.2: Animation with MotionLayout, Show changes—animating between states lets the user naturally track changes in your UI. Draw attention—use animations to draw attention to important UI  android:delay=”15%" Adds a start delay for each item that’s based on the duration of the item animation. 0% will cause all items in the layout to animate simultaneously, and 100% will let each item finish it’s animation before the next one is started. In this case, 15% of item A’s animation will pass before item B starts its animation.

margin, It is a shorthand for margin-top, margin-right, margin-bottom, and margin-left. The margin property may be specified using one, two, three, or four values. Working Draft, No significant change. Animation type, a length Firefox Android Full support 4, Opera Android Full support 14, Safari iOS Full  Left-to-Right Animation. In order to make items enter from left to right, let’s add following to your code. Make sure to update your DURATION to equal 150.

  • Don't forget to set the duration, I had some issues with it until I set the duration: a.setDuration(duration);
  • I get an error when I try to use params.leftMargin . Apparently it's not a field
  • It's because there are a lot of LayoutParams classes. For example - ViewGroup.LayoutParams, FrameLayout.LayoutParams, LinearLayout.LayoutParams, etc. ViewGroup.LayoutParams doesn't have leftMargin field but RelativeLayout.LayoutParams does.
  • According to…, the default duration is 0ms, so you must set duration as well.
  • What if I wanted newLeftMargin to be 0 ?
  • You can animate the margin from negative to 0 using Animation class. There are no any limitation.
  • @user1991679 and how do you animate from say 400 to 200 using the Animation class?
  • Assume that 200 is newMargin (the margin we should animate to) and 400 is currentMargin (the marging we animete from). In that case the code would look like: @Override protected void applyTransformation(float interpolatedTime, Transformation t) { LayoutParams params = yourView.getLayoutParams(); params.leftMargin = currentMargin - (int)((currentMargin - newMargin) * interpolatedTime); yourView.setLayoutParams(params); }
  • I get strange redraws with this. Looks like my layout bobbles a lot.
  • @MikeMilla Use it inside a FrameLayout, LinearLayout produces bad effect.
  • I tried this. And while the result is the correct position, it doesn't do the animation anymore. Any idea? If I go back to the non calculated thing the animation works fine..
  • You can set a break point when directly after the start point is set, so you can see what is entered. If it jumps, it would mean, that you are assigning the wrong value. In my code snippet, the bottomMarginStart is read from the view, assuming that the view should start the animation where it is.