androidmaterial-components-androidandroid-textinputlayout

AutoCompleteTextView dropdown not showing after device rotation


I have the following AutoCompleteTextView:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/offering_type_dropdown_layout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/date_card_spacing"
    android:layout_marginStart="4dp"
    app:layout_constraintStart_toEndOf="@+id/offering_details_header_image"
    app:layout_constraintEnd_toStartOf="@+id/offering_details_date_layout"
    app:layout_constraintTop_toTopOf="parent"
    android:hint="@string/offering_type_hint">
    <AutoCompleteTextView
        android:id="@+id/offering_details_type_dropdown"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="textNoSuggestions"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:cursorVisible="false"/>
</com.google.android.material.textfield.TextInputLayout>

In my Activity's onCreate, I fill the AutoCompleteTextView like this:

    String[] TYPES = new String[] {getString(R.string.burnt_offering), getString(R.string.meal_offering), getString(R.string.peace_offering), getString(R.string.sin_offering)};
    ArrayAdapter<String> adapter = new ArrayAdapter<>(OfferingInputActivity.this, R.layout.offering_types_dropdown, TYPES);
    mOfferingTypeCombo.setAdapter(adapter);

Then I populate the view using a Room database and preselect one of the values. In the Room callback, I do:

  mOfferingTypeCombo.setText(getString(R.string.meal_offering)), false);

Everything works well on the initial run, and the dropdown is shown correctly:

enter image description here

Now I rotate the device to landscape. The very same code as above is executed but this time, the dropdown box only shows the current selection:

enter image description here

For some reason, all other entries in the adapter have disappeared. I have tried hacks such as setAdapter(null) before I set the adapter, but no success. Can someone tell me why after rotation, the dropdown is missing entries even though the exact same code is executed?


Solution

  • Currently there is a open bug on this topic.

    You can use as workaround the setFreezesText method:

    AutoCompleteTextView autoCompleteTextView =
        view.findViewById(R.id.offering_details_type_dropdown);
    autoCompleteTextView.setFreezesText(false);
    

    The EditText set the freezesText=true. Due to this value after the rotation the TextView#onRestoreInstanceState(Parcelable) calls autoCompleteTextView.setText(value,true) which applies a filter to the adapter values.