1

I have array that populate its item from JSON using this method

JSONObject dataProvinsi = new JSONObject(data.getString("data"));
ArrayList<CharSequence> arrKota = new ArrayList<>();
JSONArray jsArrKota = dataProvinsi.getJSONArray("kota");
if(jsArrKota.length() > 0) {
    for (int i = 0; i < jsArrKota.length(); i++) {
        arrKota.add(jsArrKota.get(i).toString());
    }
}

Then my spinner populate its item from those array. This is my code to populate spinner item

ProvinsiAdapter provinsiAdapter = new ProvinsiAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, arrKota);
spRujukanKota.setAdapter(provinsiAdapter);

I created my own adapter class

public class ProvinsiAdapter extends ArrayAdapter<CharSequence> {
    private final Context context;
    private final ArrayList<CharSequence> itemsArrayList;

    public ProvinsiAdapter(Context context,int resource, ArrayList<CharSequence> itemsArrayList) {

        super(context, android.R.layout.simple_spinner_item, itemsArrayList);

        this.context = context;
        this.setDropDownViewResource(resource);
        this.itemsArrayList = itemsArrayList;
    }
}

My spinner works well, but there is some problem I am facing. My spinner text goes white and I can't change its color into black. My question is, how I can change spinner text color? Any help would be appreciated. Thank you.

Sonic Master
  • 1,238
  • 2
  • 22
  • 36

1 Answers1

0

Because you are using the native layout which id called: "android.R.layout.simple_spinner_item" system will display this layout with its specifications.

So what you have to do?!

you have two choices:

A- to open this layout and look for the id of the textView which is "R.id.text1" then override "getView()" method, which is in your custom adapter, and change text's color using this code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
    }
    TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
    textView.setTextColor(R.color.your_color);

    return convertView;
}

Or: B- you can do your work your way (Custom adapter with custom layout) and you will override getView same way too.

You can take a look in this link for further clarification : how to change spinner text size and text color

Community
  • 1
  • 1
Mohammad Sayed
  • 146
  • 1
  • 8
  • Case A: Which class should I override getView method? Case B: I tried using this method by create adapter like ArrayAdapter adapter = new ArrayAdapter(this, R.layout.spinner_item,list); but it gives me error **cannot resolve constructor array adapter ... ** this error said it was related to Volley lib that I am using. Does my adapter creation goes right? – Sonic Master Sep 10 '15 at 01:55
  • in Case A: just override getView method in your custom adapter as I mentioned, in this case it is your "ProvinsiAdapter" in Case B: change the ArrayAdapter with BaseAdapter and every thing will go fine. But you have to take care of "itemsArrayList" by your self. – Mohammad Sayed Sep 10 '15 at 13:01
  • Really thank you, Mohammad Sayed. It solved by following case B. Creating new style and using it as layout on adapter. – Sonic Master Sep 11 '15 at 02:52
  • Welcome @SonicMaster ^_^ it's a pleasure ^_^ – Mohammad Sayed Sep 12 '15 at 05:42