Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner Item Layout for Default Entries in XML

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.

like image 563
mavrosxristoforos Avatar asked Jan 15 '17 20:01

mavrosxristoforos


2 Answers

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

like image 67
Moinkhan Avatar answered Oct 03 '22 06:10

Moinkhan


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!

like image 33
SAM_U_RAI Avatar answered Oct 03 '22 08:10

SAM_U_RAI