Android Spinner Item Layout for Default Entries in XML
Asked Answered
G

2

5

I have two Spinners. One of them is populated on run-time.

groupSpinner.setAdapter(new ArrayAdapter<>(this,
                 android.R.layout.simple_spinner_item, groupNames));

The other one is pre-populated in the XML layout using a String array.

<Spinner ...
    android:entries="@array/my_items_here" />

Both Spinners appear fine. However, when their items are shown, the children layouts do not match, both in dropdown and in dialog mode. I must be missing something very simple, but how can I set them to use the same layout (hopefully android.R.layout.simple_spinner_item), without creating my own custom layout, or loading the XML String array on run-time?

It seems a basic thing to do, but I cannot find an answer and I have searched a lot, already.

See screenshots, below:

Programmatically Populated Spinner XML Pre-populated Spinner

*Please forgive the use of Greek characters in the second image. I have checked and confirmed that the problem is not related to the use of Greek characters.

Gravitt answered 15/1, 2017 at 20:36 Comment(14)
Both the spinner layout looks same except the width.Mauricio
The items in the dialogs don't look the same. I have never set the second ones to be bold, anywhere. That's just the default way to show them. However, as you can see, the first dialog, which is also using the default way, is much better and not so rough. So, I am searching for a way to set the item layout for the second dialog, without loading them on run-time.Gravitt
Can you post code for both the spinners.Mauricio
I have posted code for both spinners. The second one is inflated with the layout, pre-filled from the XML, as shown.Gravitt
use android.R.layout.simple_spinner_dropdown_item instead of android.R.layout.simple_spinner_itemHochheimer
@Hochheimer Thank you for your suggestion. I just tried it and the result was not better. In fact, it made the Spinner item of the first Spinner, look different from the other, within the Spinner itself. The question is, how to change the item layout for the XML pre-filled one? It doesn't seem to offer a way to manipulate that, unless you manually create the Spinner adapter.Gravitt
ok let me delete the answer first so that it will not mislead other people.Hochheimer
@Hochheimer Don't worry, the answer is only visible to you. I saw it just for a minute, when you posted it.Gravitt
Have you tried using adapter.setDropDownViewResource()? Also, are both under the same theme/style?Culberson
try this ((ArrayAdapter)preFilledSpinner.getAdapter()).setDropDownViewResource(android.R.layout.simple_spinner_item);Hochheimer
@Hochheimer That is the answer. Post it and I will accept it and award the bounty.Gravitt
@AmrAbed Thank you. Your comment was very valuable. I almost found it, but Moinkhan posted the exact answer.Gravitt
Obviously @Hochheimer just used my comment to get his answer and you preferred to give him the creditCulberson
@AmrAbed I am sorry that you feel this way. However, your comment asked if I had used a function on an adapter and I was specifically asking not to use a custom adapter. I have found that function in my search, and also tried to figure it out now, again, but casting the getAdapter of the Spinner was the part that actually solved it for me. Moreover, Moinkhan had been around for an hour.Gravitt
H
8

try this ((ArrayAdapter)preFilledSpinner.getAdapter()).setDropDownVie‌wResource(android.R.‌​layout.simple_spinne‌​r_item);

To know how it works just check the code of AppCompatSpinner and below is the default code of AppCompatSpinner to figure out how it works when you pass the entries.

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 are passing entries through XML they will created a ArrayAdapter and apply code adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); where you can see, R.layout.support_simple_spinner_dropdown_item is set by default, which should be applicable from parameter but unfortunately they have make it fixed

Hochheimer answered 22/1, 2017 at 11:44 Comment(3)
Thank you @Moinkhan. You deserve the bounty for your time. Please edit the question a little bit, so that it includes some information on why it is correct, and it would be much prettier.Gravitt
The code where you create the entries is non-relevant to the question and incorrect, because you set the DropDown View Resource directly in the new ArrayAdapter<>() constructor and there is no need to set it again. Avoid re-creating the problem in your answer, because that may be misleading.Gravitt
@Gravitt it's default code of AppCompatSpinner I have edited answer.Hochheimer
D
1

That how I done it in my recent project. Java Code:

public void spinner_settings() {

    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    adapter =
            ArrayAdapter.createFromResource(this, R.array.activity_list, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            switch(position){
                case 0:
                    // TO-DO something when item selected
                    break;
                case 1:
                    // TO-DO something when item selected
                    break;
                case 2:
                    // TO-DO something when item selected
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });
}

res-> values -> strings.xml

<resources>
    <string-array name="activity_list">
        <item>MainFrame</item>
        <item>Settings</item>
        <item>Translation Rules</item>
    </string-array>
</resources>

res-> values -> styles.xml

<resources>
 <!-- For the resting Spinner style -->
        <item name="android:spinnerItemStyle">
            @style/spinnerItemStyle
        </item>

        <!-- For each individual Spinner list item once clicked on -->
        <item name="android:spinnerDropDownItemStyle">
            @style/spinnerDropDownItemStyle
        </item>

    </style>

    <style name="spinnerItemStyle">
        <item name="android:textSize">23sp</item>
        <item name="android:textColor">#000000</item>
        <item name="android:background">#008080</item>
    </style>

    <style name="spinnerDropDownItemStyle">
        <item name="android:padding">5sp</item>
        <item name="android:textSize">25sp</item>
        <item name="android:textColor">#000000</item>
    </style>
    </resources>

I hope it will help you!

Dedradedric answered 22/1, 2017 at 13:39 Comment(1)
Thank you for your answer. The question is not about how to inflate a Spinner using a XML array on run-time.Gravitt

© 2022 - 2024 — McMap. All rights reserved.