Last active
March 26, 2019 15:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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