It is very easy. I too added it in my chat app and it works like a charm. In your root layout, add this line:
android:animateLayoutChanges="true"
This will animate all the changes happening in the layout.
Edit
If that does not help, you can do this.
userIdInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_ENTER) {
ValueAnimator anim = ValueAnimator.ofInt(edMESSAGE.getHeight() - 40, edMESSAGE.getHeight() + 40)
.setDuration(500);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
edMESSAGE.getLayoutParams().height = (int)animation.getAnimatedValue();
edMESSAGE.requestLayout();
}
});
anim.start();
}
return false;
}
});
You can call this method in onKeyListener
and when the new line key is pressed, this code can be called.
Edit
Sometimes, while entering the text, it goes to new line. For animation this, we can do this:
chatInputET.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String string = s.toString();
if (string.length() > 0 && string.charAt(string.length() - 1) == '\n') {
ValueAnimator anim = ValueAnimator.ofInt(edMESSAGE.getHeight() - 40, edMESSAGE.getHeight() + 40)
.setDuration(500);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
edMESSAGE.getLayoutParams().height = (int)animation.getAnimatedValue();
edMESSAGE.requestLayout();
}
});
anim.start();
}
}
});
And for the backspace key, you can do this:
inputMessage.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
String string = inputMessage.getText().toString();
if ((keyCode == KeyEvent.KEYCODE_DEL && (string.length() > 0 && string.charAt(string.length() - 1)) {
ValueAnimator anim = ValueAnimator.ofInt(edMESSAGE.getHeight() + 40, edMESSAGE.getHeight() - 40)
.setDuration(500);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
edMESSAGE.getLayoutParams().height = (int)animation.getAnimatedValue();
edMESSAGE.requestLayout();
}
});
anim.start();
}
return false;
}
});