Skip to content

Instantly share code, notes, and snippets.

@frett
Created May 15, 2019 17:53
Show Gist options
  • Save frett/d6a2dbdc9718444847d00768fbaeacf5 to your computer and use it in GitHub Desktop.
Save frett/d6a2dbdc9718444847d00768fbaeacf5 to your computer and use it in GitHub Desktop.
Android Material Components - Dropdown Menu + Data Binding
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.annotation.LayoutRes
import androidx.databinding.BindingAdapter
@BindingAdapter("dropDownItems", "dropDownItemLayout", "dropDownItemsIncludeEmpty", requireAll = false)
fun AutoCompleteTextView.setItems(items: Array<String>?, @LayoutRes layout: Int?, includeEmpty: Boolean?) =
setAdapter(
ArrayAdapter(
context,
layout ?: R.layout.default_dropdown_menu_popup_item,
(if (includeEmpty == true) arrayOf("") else emptyArray()) + (items ?: emptyArray())
)
)
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/contact_editor_hint_status"
app:layout_constraintTop_toBottomOf="@id/name">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="@{999999}"
android:editable="false"
android:text="@={contact.translatedStatus}"
app:dropDownItemLayout="@{@layout/mtrl_dropdown_menu_popup_item}"
app:dropDownItems="@{Constants.statusValues}"
app:dropDownItemsIncludeEmpty="@{true}"
tools:text="Partner" />
</com.google.android.material.textfield.TextInputLayout>
@bosankus
Copy link

Can you please provide the code for Constants.statusValues ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment