5

On a view I have a LinearLayout which I want to collapse when I click on the EditText below and then to expand when we unfocus.

<EditText
            android:layout_width="1000dp"
            android:layout_height="43dp"
            android:layout_centerVertical="true"
            android:textSize="14.4sp"
            android:gravity="center_vertical"
            android:hint="@string/hint_query"
            android:layout_toEndOf="@+id/searchImage"
            android:id="@+id/searchBoxText"
            android:background="@null"
            android:layout_marginStart="16dp"
            android:inputType="text"
            tools:ignore="Autofill" />

I have a onFocusChangeListener that I implemented thanks to Android-Annotations.

@FocusChange
void searchBoxText(EditText searchBoxText) {
    Log.d("change focus", "focus has changed with " + searchBoxText.hasFocus());
    if (!searchBoxText.hasFocus()) {
        if(upperView != null)
            upperView.setVisibility(View.VISIBLE);
    } else {
        if(upperView != null)
            upperView.setVisibility(View.GONE);
    }
}

And I got a touchListener on the parent which throws :

searchBoxText.clearFocus();

when we click out of the EditText.

The tablet I aim this code for support as the maximum API 24. My problem is that this code works perfectly in API 28 but not on API 24 where it throws the onFocusChange twice and I didn't find any reason as why it does it or any way to make it work.

Florian Saurs
  • 111
  • 2
  • 3
  • Have you tried calling requestFocus() on a different View rather than calling clearFocus()? https://stackoverflow.com/questions/14424654/how-to-clear-focus-for-edittext – Gavin Wright Oct 31 '18 at 12:54
  • @GavinWright yes I did try but I have nothing else to focus on and the upperView is a Linear so requestFocus is not fired on him – Florian Saurs Oct 31 '18 at 13:28
  • Try this: https://stackoverflow.com/questions/4828636/edittext-clear-focus-on-touch-outside – Gavin Wright Oct 31 '18 at 13:32
  • @GavinWright this is actually the method I'm using except that mEditText.clearFocus() throws onFocusChange on my edit text twice on API 24 but not on API 28. – Florian Saurs Oct 31 '18 at 14:25
  • Can't you just ignore the second onFocusChange event? If the event fires and the LinearLayout is already collapsed/collapsing, just do nothing. – Gavin Wright Oct 31 '18 at 15:25
  • I cannot because there is nothing which differ the first call from the second and I need this function to work in both way. – Florian Saurs Oct 31 '18 at 15:28

2 Answers2

1

I think what happens is:

  1. The user leaves the first item (so focus is going off from first item so focus is "Changing" so the event onFocuseChange trigers.

  2. Then we get the focus on the selected seconed item so again "focusCahnged" so again the event onFocuseChange called. so you can do :

                    public void onFocusChange(View v, boolean b) {
                               if (v.hasFocus()){
                                     v.performClick()); // or any other logic you need
                               } 
                     }
    

    then it will do what you need only once (when v will get the focus) .

good luck :-)

Udi Reshef
  • 1,083
  • 1
  • 11
  • 14
1

You can add isFocusable and FocusableInTouchMode in xml for another view

Mohammad Davari
  • 430
  • 3
  • 13