Reputation: 111
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.
Upvotes: 5
Views: 995
Reputation: 450
You can add isFocusable and FocusableInTouchMode in xml for another view
Upvotes: 1
Reputation: 1283
I think what happens is:
The user leaves the first item (so focus is going off from first item so focus is "Changing" so the event onFocuseChange trigers.
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 :-)
Upvotes: 1