How to populate dropdown on MaterialComponents.TextInputLayout

Viewed 633

I know how to populate spinner, but I can';t get around (Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu) any idea how please ?

My resource like this

    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
    </string-array>

My Layout
 
<com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="12dp"
            android:hint="@string/select_type"
            android:clickable = "true"
            android:focusable = "true"
            android:focusableInTouchMode = "false"
            android:inputType = "none"
            >
 
            <com.google.android.material.textfield.MaterialAutoCompleteTextView
                style="@style/TextInputLayoutStyle"
                android:inputType="none" />

        </com.google.android.material.textfield.TextInputLayout>
   MaterialAutoCompleteTextView MySpinner = (MaterialAutoCompleteTextView) findViewById(R.id.spinner1);

      ArrayAdapter Myadapter = new ArrayAdapter<>(this,
        android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.planets_array));
      Myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

   MySpinner.setAdapter(Myadapter);

Thank you

1 Answers

You have to populate with the adapter the AutoCompleteTextView not the TextInputLayout:

 <com.google.android.material.textfield.MaterialAutoCompleteTextView
  android:id="@+id/autocomplete"

and:

    AutoCompleteTextView autocomplete = view.findViewById(R.id.autocomplete);
    autocomplete.setAdapter(adapter);
Related