How to avoid cancellation of animator effect when soft keyboard appears?

In my app some views are resized by Animator and everything is OK till the soft keyboard appears. When I tap on EditText after animation finish the height of EditText backs to xml height right before the soft keyboard appears.

The details: Let say that we have a simple layout with one button and one text edit box:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:text="start anim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/text"
        android:layout_below="@id/button"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:text="Hello World!" />
</RelativeLayout>

And simple animator:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:propertyName="bottom"
    android:duration="500"
    android:valueTo="200dp"
    android:valueFrom="56dp"
    android:repeatCount="0"
    android:valueType="intType"
    />
</set>

And then when on click event on button I run the animation:

final EditText editText = (EditText) findViewById(R.id.text);
Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        AnimatorSet show = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.expand_edit_text);
        show.setTarget(editText);
        show.start();
    }
});

Till now is OK. But when I tap on EditText and soft keyboard appears then EditText backs to its previous height . How to avoid that effect? And make animation persistent/permanent?

I found similar question without response Android showing keyboard cancels animation


Call clearAnimation() on whichever View you called startAnimation() .

How to check keyboard visibility:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO)
    {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}
链接地址: http://www.djcxy.com/p/93432.html

上一篇: 显示或隐藏Android软键盘时调整布局

下一篇: 软键盘出现时如何避免取消动画效果?