-
-
Save carlosezam/fa76d73252970824dd72ba7b2087b681 to your computer and use it in GitHub Desktop.
AutoCompleteTextView databinding
This file contains hidden or 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
| @BindingAdapter("valueAttrChanged") | |
| fun AutoCompleteTextView.setListener(listener: InverseBindingListener?) { | |
| this.onItemSelectedListener = if (listener != null) { | |
| object : AdapterView.OnItemSelectedListener { | |
| override fun onNothingSelected(parent: AdapterView<*>?) { | |
| listener.onChange() | |
| } | |
| override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { | |
| listener.onChange() | |
| } | |
| } | |
| } else { | |
| null | |
| } | |
| } | |
| @get:InverseBindingAdapter(attribute = "value") | |
| @set:BindingAdapter("value") | |
| var AutoCompleteTextView.selectedValue: Any? | |
| get() = if (listSelection != ListView.INVALID_POSITION) adapter.getItem(listSelection) else null | |
| set(value) { | |
| val newValue = value ?: adapter.getItem(0) | |
| setText(newValue.toString(), true) | |
| if (adapter is ArrayAdapter<*>) { | |
| val position = (adapter as ArrayAdapter<Any?>).getPosition(newValue) | |
| listSelection = position | |
| } | |
| } | |
| @BindingAdapter("entries", "itemLayout", "textViewId", requireAll = false) | |
| fun AutoCompleteTextView.bindAdapter(entries: Array<Any?>, @LayoutRes itemLayout: Int?, @IdRes textViewId: Int?) { | |
| val adapter = when { | |
| itemLayout == null -> { | |
| ArrayAdapter(context, R.layout.item_dropdown, R.id.dropdownText, entries) | |
| } | |
| textViewId == null -> { | |
| ArrayAdapter(context, itemLayout, entries) | |
| } | |
| else -> { | |
| ArrayAdapter(context, itemLayout, textViewId, entries) | |
| } | |
| } | |
| setAdapter(adapter) | |
| } |
This file contains hidden or 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
| <com.google.android.material.textfield.TextInputLayout | |
| android:id="@+id/tilGender" | |
| style="@style/AppTheme.DropDownMenu" | |
| android:prompt="@string/label_gender" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@id/tilLastName"> | |
| <AutoCompleteTextView | |
| android:hint="@string/label_gender" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| entries="@{@stringArray/genders}" | |
| value="@={viewModel.gender}" | |
| android:editable="false"/> | |
| </com.google.android.material.textfield.TextInputLayout> |
This file contains hidden or 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
| <style name="AppTheme.DropDownMenu" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"> | |
| <item name="android:layout_width">0dp</item> | |
| <item name="android:layout_height">wrap_content</item> | |
| <item name="android:layout_marginTop">@dimen/margin_regular</item> | |
| <item name="android:layout_marginBottom">@dimen/margin_regular</item> | |
| <item name="android:layout_marginStart">@dimen/margin_large</item> | |
| <item name="android:layout_marginEnd">@dimen/margin_large</item> | |
| <item name="hintTextColor">@color/colorpalette_blue</item> | |
| <item name="boxStrokeColor">@color/outlined_stroke_color</item> | |
| </style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment