0

My view got 3 item, EditText, ImageView and TextView. now i'm doing some animation base on a tutorial which was working fine, now in that tutorial guy was using onClickListener to animate view and hide image, and was working fine, but my problem is, i want to show image when user is not on EditText anymore.

Right the idea is working like this

Click on EditText => Gonna animate and hide image => and when u tap or click on layout/activity/view/etc. image won't comeback.

Code :

    btnInvite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            titleInvite.animate().translationY(-350).setDuration(800).setStartDelay(100).start();
            subtitleInvite.animate().translationY(-350).setDuration(800).setStartDelay(100).start();
            inputInvite.animate().translationY(-350).setDuration(800).setStartDelay(200).start();
            btnInvite.animate().alpha(1).translationY(-350).setDuration(800).setStartDelay(300).start();
            imageView.startAnimation(disapear);
            imageView.setVisibility(View.INVISIBLE);

        }
    });

This is what going to happen when someone click on EditText now i want to reverse it when edit text is not focused anymore, or something else clicked. I'm also using API 17, and i'm telling this bcs i looked into some of features but most of them couldn't be used in 17.

Mohammad Eskandari
  • 221
  • 1
  • 5
  • 14

1 Answers1

1

You need to add a listener for loosing focus on editText, in that listener execute the reverse animation.

EditText txtEdit = (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
  @Override public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus) { 
      // Execute reverse animation
    } 
  } 
});
Sander Rito
  • 396
  • 2
  • 8
  • 1
    And actually if the animation gas to happen when the textView is focused, you van move the animation code to the same onFocusChange() method – Sander Rito Mar 01 '19 at 04:42
  • Thanks. there are 2 problem if i use onClickListener i'm gonna get click sound when i tap on it. and when activity open edit text is focused which gonna do animation on run bcs edit text is focused. and i can't make it false since i'm using api 17(setDefaultFocus is for api 21 above), any idea? – Mohammad Eskandari Mar 01 '19 at 05:06
  • 1
    There are ways to avoid the editText to be selected on activity creation. I suggest you search for them. Heres an example ive used in the past https://stackoverflow.com/a/1662088/3885877 – Sander Rito Mar 01 '19 at 05:09