I want to create spinner styled with material design. My problem is when I have 2 spinners in one layout then on phone rotation selection on spinner 2 is picked in spinner 1.
For example:
Spinner1 has items test1,test2,test3.
Spinner2 has items test4,test5,test6.
When I select test2 on Spinner1 and test5 on Spinner2 and rotate phone then on Spinner1 selected item is test5 which shouldn't even be possible. It happens only on my test device Samsung S9. When I am doing the same thing on other phones like OnePlus or Pixel emulator then Spinner1 selection stay as it is test2.
MainActivity code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestSpinner testTestSpinner1 = findViewById(R.id.testSpinner1);
testTestSpinner1.setItems(Arrays.asList("test1", "test2", "test3"));
TestSpinner testTestSpinner2 = findViewById(R.id.testSpinner2);
testTestSpinner2.setItems(Arrays.asList("test4", "test5", "test6"));
}
activity_main code
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<com.skolimowski.testexposeddropdownmenu.TestSpinner
android:id="@+id/testSpinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.skolimowski.testexposeddropdownmenu.TestSpinner
android:id="@+id/testSpinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/testSpinner1" />
TestSpinner code
public class TestSpinner extends FrameLayout {
private ArrayAdapter<String> adapter;
public TestSpinner(@NonNull Context context) {
super(context);
init(context);
}
public TestSpinner(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TestSpinner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
FrameLayout parentLayout = (FrameLayout) inflate(context, R.layout.spinner_layout, this);
TextInputLayout textInputLayout = parentLayout.findViewById(R.id.til);
AutoCompleteTextView autoCompleteTextView = textInputLayout.findViewById(R.id.ac);
adapter = new ArrayAdapter<>(context, android.R.layout.simple_dropdown_item_1line);
autoCompleteTextView.setAdapter(adapter);
}
public void setItems(List<String> items) {
adapter.clear();
adapter.addAll(items);
}
}
spinner_layout code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:id="@+id/ac"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>