Skip to content

Instantly share code, notes, and snippets.

@Audhil
Last active January 10, 2025 16:02
Show Gist options
  • Save Audhil/c1705dc1cfceff4cd8e9aeaabb05fbb6 to your computer and use it in GitHub Desktop.
Save Audhil/c1705dc1cfceff4cd8e9aeaabb05fbb6 to your computer and use it in GitHub Desktop.
make autocompletetextview as spinner with textInputLayout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/base_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Hint text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<AutoCompleteTextView
android:id="@+id/outlined_exposed_dropdown_editable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:hint="text"
android:text="jack" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
class DemoActivity : AppCompatActivity() {
private val fruits by lazy {
arrayOf("Pomogranate", "Banana", "Mango", "Sappota", "Grape")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_demo)
outlined_exposed_dropdown_editable.run {
setOnClickListener {
setAdapter(ArrayAdapter(applicationContext, android.R.layout.simple_spinner_dropdown_item, fruits))
// setTextColor(ContextCompat.getColor(context, R.color.primary_red))
}
inputType = InputType.TYPE_NULL
performClick()
setOnItemClickListener { _, _, position, _ ->
println("yup selected Item: ${fruits[position]}")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment