0

I have a spinner in a Layout, it is defined as

<Spinner
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/bithYearSpinner"
      android:layout_marginTop="15dp" />

Spinner declaration and Adapter:

birthYearSpinner = (Spinner) findViewById(R.id.bithYearSpinner);
String[] years = getResources().getStringArray(R.array.bithYears);
ArrayAdapter<String> adapter = new ArrayAdapter<String(this,android.R.layout.simple_spinner_dropdown_item,years);
birthYearSpinner.setAdapter(adapter);

The problem is, spinner shows up with white text on white background which is impossible to see its items. I have done a research about it, some people have the same problem and there are some solutions but they dont work out for me. How can i fix this ? Any help would be appreciated.

Tartar
  • 5,149
  • 16
  • 63
  • 104

3 Answers3

0

To fix it, you can use custom xml layout for spinner item. By doing this, you can customize your spinner item text color and background.

Create item_spinner.xml and then,

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="19sp"  
    android:textColor="#111111"         
    android:padding="8dp"
    />

And set your custom layout to adapter like this :

birthYearSpinner = (Spinner) findViewById(R.id.bithYearSpinner);
String[] years = getResources().getStringArray(R.array.bithYears);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, years);
birthYearSpinner.setAdapter(adapter);

Edit

If it is in fragment, try to replace this with getActivity() . Please look at this answer for reference.

Community
  • 1
  • 1
Arkar Aung
  • 3,554
  • 1
  • 16
  • 25
0

create custom rowlayout for spinner and name it as spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/theme_color"
    android:orientation="vertical">

    <TextView
        android:id="@+id/spinnerText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#ff00ff"
        android:gravity="center"
        android:paddingBottom="5dip"
        android:paddingTop="5dip"
        android:text=""
        android:textColor="#000000"
        android:textSize="18sp" />
</LinearLayout>

now create Adapter and name it as DropDownAdapter.java

package com.ronem.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.ronem.goldsilverpricenepal.R;


public class DropDownAdapter extends ArrayAdapter<String> {
    LayoutInflater inflater;
    String[] genereValue;

    public DropDownAdapter(Context context, int textViewResourceId,
                           String[] genereValue) {
        super(context, textViewResourceId, genereValue);
        inflater = LayoutInflater.from(context);
        this.genereValue = genereValue;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        // LayoutInflater inflater = getLayoutInflater();
        convertView = inflater.inflate(R.layout.spinner_item_layou, parent, false);
        TextView label = (TextView) convertView.findViewById(R.id.spinnerText);
        label.setText(genereValue[position]);


        return convertView;
    }
    }

now initialize and set custom adapter as shown in your activity

DropDownAdapter dAdapter = new DropDownAdapter(this, R.layout.spinner_item_layou, spinnerValues);
        itemSpinner.setAdapter(dAdapter);
Ram Mandal
  • 1,899
  • 1
  • 20
  • 35
0

When i change my resource array <array name=""> to <string-array name="">, spinner items show up properly and no need to use a custom adapter.

Tartar
  • 5,149
  • 16
  • 63
  • 104