Android, spinner items not showing

user3139428 picture user3139428 · Apr 4, 2017 · Viewed 13.7k times · Source

This is my code and I can not get the items add inside Spinner. I don't know what's going wrong, and unable to find any other way!

Java:

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

 String[] datos = getResources().getStringArray(R.array.items);

 ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, datos);
adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 spinner.setAdapter(adaptador);

XML:

<Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_margin="26dp"
        android:textColor="#FFF"/>

strings.xml:

<string-array name="items">
    <item >Item 1</item>
    <item >Item 2</item>
    <item >Item 3</item>
    <item >Item 4</item>
</string-array>

Thanks in advance for the help.

Answer

rafsanahmad007 picture rafsanahmad007 · Apr 4, 2017

your spinner text color is white: change it to other color.

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_margin="26dp"
    android:textColor="#000000"/>  //change this

also you can directly access the array resource in your layout. Like:

android:entries="@array/items" //add this in your spinner layout

EDIT

you can try using a custom layout for your spinner item:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"
    android:background="#000000"  //dark background
    android:text="Test"
    android:textColor="#ffffff"  //white text
    android:padding="5dp"
    />

use the layout using:

ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
        R.layout.spinner_item, datos);

here R.layout.spinner_item is the spinner custom layout