Skip to content

Instantly share code, notes, and snippets.

@fonix232
Last active August 16, 2021 07:43
Show Gist options
  • Select an option

  • Save fonix232/6553f66d17746dca5d062eecf40e8b3b to your computer and use it in GitHub Desktop.

Select an option

Save fonix232/6553f66d17746dca5d062eecf40e8b3b to your computer and use it in GitHub Desktop.

Revisions

  1. fonix232 revised this gist May 7, 2019. 2 changed files with 27 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions Layout.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <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>
    10 changes: 10 additions & 0 deletions style.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <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>
  2. fonix232 created this gist May 7, 2019.
    47 changes: 47 additions & 0 deletions BindingAdapters.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    @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)
    }