Skip to content

Instantly share code, notes, and snippets.

@RobertZagorski
Last active March 26, 2019 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RobertZagorski/1cc49332f743db74b8b75d878bf798ba to your computer and use it in GitHub Desktop.
Save RobertZagorski/1cc49332f743db74b8b75d878bf798ba to your computer and use it in GitHub Desktop.
AutoCompleteTextView CoordinatorLayout.Behavior that is aware of Snackbar and changes autocompletes popup when Snackbar is shown.
package com.example.rzagorski.coordinatorawareautocompletetextviewapp;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.ListPopupWindow;
import java.lang.reflect.Field;
/**
* Created by Robert Z on 05.06.2017.
*/
public class AutoCompleteTextViewBehaviour extends CoordinatorLayout.Behavior<AutoCompleteTextView> {
View popupList;
public AutoCompleteTextViewBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, AutoCompleteTextView child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, final AutoCompleteTextView child, View dependency) {
if (popupList == null) {
popupList = getPopupList(child);
if (popupList == null) {
return super.onDependentViewChanged(parent, child, dependency);
}
}
int dropdownBottom = child.getBottom() + child.getDropDownVerticalOffset() + popupList.getHeight();
int snackBarTop = dependency.getTop();
int difference = dropdownBottom - snackBarTop;
if (difference > 0) {
child.setDropDownHeight(popupList.getHeight() - difference);
return true;
} else {
child.setDropDownHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
}
return super.onDependentViewChanged(parent, child, dependency);
}
@Nullable
private View getPopupList(AutoCompleteTextView child) {
try {
Field popupField;
Class clazz;
if (child instanceof AppCompatAutoCompleteTextView) {
clazz = child.getClass().getSuperclass();
} else {
clazz = child.getClass();
}
popupField = clazz.getDeclaredField("mPopup");
popupField.setAccessible(true);
ListPopupWindow popup = (ListPopupWindow) popupField.get(child);
Field popupListViewField = popup.getClass().getDeclaredField("mDropDownList");
popupListViewField.setAccessible(true);
return (View) popupListViewField.get(popup);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_marginTop="56dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="de"
app:layout_behavior="com.example.rzagorski.coordinatorawareautocompletetextviewapp.AutoCompleteTextViewBehaviour"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment