Android Spinner project layout for default entries in XML
I have two spinner widgets. One of them is populated at run time.
groupSpinner.setAdapter(new ArrayAdapter<>(this,
android. R.layout.simple_spinner_item, groupNames));
The other uses an array of strings to pre-populate the XML layout.
<Spinner ...
android:entries="@array/my_items_here" />
Both spinner looks good. However, when their items are displayed, the child layouts do not match in both dropdown
and dialog
modes.
I must have missed something very simple, but how can I set them up to use the same layout (hopefully android. R.layout.simple_spinner_item
), or do I load an array of XML strings at runtime?
It seems like a basic thing, but I can’t find the answer, and I’ve searched a lot.
See screenshot below
*Please forgive the use of Greek characters in the second picture. I have checked and confirmed that the problem is not related to the use of Greek characters.
Solution
Try this ((ArrayAdapter)preFilledSpinner.getAdapter()).setDropDownVie wResource(android.R layout.simple_spinne r_item);
To understand how it works, just examine the code for AppCompatSpin,
below is the default code for AppCompatSpinner
to see how it works when you pass an entry
final CharSequence[] entries = a.getTextArray(R.styleable.Spinner_android_entries);
if (entries != null) {
final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context,android. R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
setAdapter(adapter);
}
When we pass the entry through XML, they will create an ArrayAdapter
and apply the code adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
You can see that R.layout.support_simple_spinner_dropdown_item
is the default setting, which should work for the parameters but unfortunately they have been fixed