0

I am currently creating android app. I have a login activity in which on top there is LinearLayout with ImageViewand below it there is GirdLayout where there are few EditTexts. I would like to move my LinearLayout to given height when any of the EditTexts has focus. I was trying the solution from here: Android translate animation - permanently move View to new position using AnimationListener and put AnimationListener inside of onFocusChange, but in my case it appears to do nothing. Could You help me?

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="6"
android:background="#74B897"
android:orientation="vertical"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/imageLayout"
    >

    <ImageView
        android:layout_width="247dp"
        android:layout_height="290dp"
        android:id="@+id/logo_imageView"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:layout_row="1"
        android:src="@drawable/white_logo" />

</LinearLayout>


    <GridLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/layout_shape"
        android:layout_weight="2"
        android:layout_row="7"
        android:layout_column="1"
        android:weightSum="5"
        android:layout_below="@+id/logo_imageView"
        android:layout_marginLeft = "15dp"
        android:layout_marginRight = "15dp"
        android:layout_marginBottom = "30dp"
        >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/fname_editText"
            android:hint="First Name"
            android:layout_rowWeight="0"
            android:layout_marginTop = "15dp"
            android:layout_row="1"
            android:layout_column="0"
            android:textColor="#FFFFFF"
            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/sname_editText"
            android:layout_below="@+id/fname_editText"
            android:hint="Second Name"
            android:layout_rowWeight="0"
            android:layout_row="2"
            android:layout_column="0"
            android:textColor="#FFFFFF"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/password_editText"
            android:inputType="textPassword"
            android:ems="10"
            android:hint="Password"
            android:layout_rowWeight="0"
            android:layout_row="3"
            android:layout_column="0"
            android:textColor="#FFFFFF"/>

        <Button
            android:layout_width="141dp"
            android:text="Log In"
            android:id="@+id/log_button"
            android:background="@drawable/button_shape"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="40dp"
            android:layout_below="@+id/id_editText"
            android:layout_centerInParent="true"

            android:layout_gravity="center_horizontal"
            android:layout_rowWeight="1"
            android:textSize="20dp"
            android:textStyle="bold"
            android:layout_row="5"
            android:layout_column="0"
            android:layout_height="29dp"
            android:textColor="#636363" />

    </GridLayout>
</LinearLayout>

Activity:

public class LoginActivity extends Activity implements View.OnFocusChangeListener{

EditText sname;
EditText password;
TextView info;
ImageView img;
EditText fname;
LinearLayout imageLayout;
int oldX, oldY, newX, newY;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_layout);

    fname = (EditText) findViewById(R.id.fname_editText);
    sname = (EditText)findViewById(R.id.sname_editText);
    password = (EditText)findViewById(R.id.password_editText);
    img= (ImageView) findViewById(R.id.logo_imageView);
    imageLayout = (LinearLayout) findViewById(R.id.imageLayout);


    oldX = imageLayout.getWidth();
    oldY = imageLayout.getHeight();


    img.setImageResource(R.drawable.white_logo);
    Button log_Button = (Button) findViewById(R.id.log_button);

    fname.setFocusable(true);
    sname.setFocusable(true);
    password.setFocusable(true);

}



@Override
public void onFocusChange(View v, boolean hasFocus) {


    if(v.getId() == R.id.fname_editText || v.getId() == R.id.sname_editText || v.getId() == R.id.password_editText)
    {
        final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 0.F, -300);
        final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 300, 0.F);

        if(hasFocus){
            //first option
            moveDownAnim.start();

            /*
            second option
           TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -300);

            anim.setFillAfter(false);
            anim.setDuration(1000);

            anim.setAnimationListener(new TranslateAnimation.AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    imageLayout.clearAnimation();
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageLayout.getLayoutParams();
                    params.topMargin += -300;
                    imageLayout.setLayoutParams(params);

                    animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, -300);
                    animation.setDuration(1);
                    imageLayout.startAnimation(animation);
                }
            });

            imageLayout.startAnimation(anim);*/
        }
    }
}

}

Community
  • 1
  • 1
XX XX
  • 23
  • 7

1 Answers1

0

I don't see you setting the OnFocusChangeListener for the EditTexts in your code.

fname.setOnFocusChangeListener(this);
sname.setOnFocusChangeListener(this);
prasunnair
  • 92
  • 3
  • 12