Home > database >  Autocomplete textview android
Autocomplete textview android

Time:12-20

I am new to android developer.I want to know it is possible to set the selected item’s background of autocomplete text view drop-down menu.I try so many ways but the color change only when click.I can’t use spinner can use only autocomplete textview for school project. Below is example photo.

enter image description here

When drop-down pop up I want selected item with background color.please help!

CodePudding user response:

Your main layout

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <AutoCompleteTextView
        android:id="@ id/autoCompleteTextView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:inputType="none"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="LabelFor" />

</com.google.android.material.textfield.TextInputLayout>

Create an drop_down_style.xml layout in your resources. Add following view...

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@ id/drop_down_text"
    style="?android:attr/dropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#C0C000"
    android:gravity="center"
    android:padding="5dp" />

Update your ArrayAdapter as following

List<String> dataList = new ArrayList<>();
dataList.add("1");
dataList.add("2");
dataList.add("3");
dataList.add("4");


AutoCompleteTextView view = findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(this, R.layout.drop_down_style, R.id.drop_down_text, dataList);
view.setAdapter(myAdapter);

CodePudding user response:

[enter image description here][1]

[1]: https://i.stack.imgur.com/pWG8x.pngstrong text

  • Related