简体   繁体   中英

How to change the text color of only first element of spinner?

I have to change the color of spinner first element which is displayed as the default element. Tried various methods not working. provide a working solution.

I checked this solution after some modifications, this answer will helps you to change first spinner of item.

In onCreate, calling common method as setSpinnerValue :

ArrayList<String> testarray = new ArrayList<String>();
        testarray.add("item0");
        testarray.add("item1");
        testarray.add("item2");
        testarray.add("item3");
        setSpinnerValue(testarray,timeSpinner,arrayAdapter);

Here, i have created Common method with arrayList , spinner and arrayAdapter as parameter for setting spinner items in adapter .

public void setSpinnerValue(final ArrayList<String> list, Spinner spinner, ArrayAdapter<String> adapter)
    {
        try {
            adapter = new ArrayAdapter<String>(TimePicker_Activity.this, android.R.layout.simple_spinner_item, list) {

                @Override
                public boolean isEnabled(int position) {
                    String val = String.valueOf(list.get(position));
                    boolean isFalse = val.contains(":False");
                    return !isFalse;
                }

                @Override
                public boolean areAllItemsEnabled() {
                    return false;
                }

                @Override
                public View getDropDownView(int position, View convertView, android.view.ViewGroup parent) {
                    View v = convertView;
                    if (v == null) {
                        Context mContext = this.getContext();
                        LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
                    }
                    TextView tv = (TextView) v.findViewById(android.R.id.text1);
                    String val = String.valueOf(list.get(position));
                    tv.setText(val.replace(":False", ""));

                    switch (position) {
                        case 0:
                            tv.setTextColor(android.graphics.Color.GRAY);
                            break;

                        default:
                            tv.setTextColor(getResources().getColor(R.color.colorPrimary));
                            break;
                    }

                    return v;
                }
            };
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setSelection(0, false);
            spinner.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Getting output as below,

For the people who struggle to get this working, this is the Kotlin solution.

  1. Subclass the ArrayAdapter class

     class CustomArrayAdapter(context: Context, resource: Int, objects: List<String>): ArrayAdapter<String>(context, resource, objects) { override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View { val view = super.getDropDownView(position, convertView, parent) if (view is TextView) { if (position == 0) { view.setTextColor(ContextCompat.getColor(parent.context, R.color.red_color_1)) } else { view.setTextColor(ContextCompat.getColor(parent.context, R.color.blue_color_1)) } } return view } }

Connect to adapter to your list:

fun setAdapter() {
    CustomArrayAdapter(this, R.layout.custom_list_item, buildItems()).also { adapter ->
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(R.layout.custom_list_item)
        // Apply the adapter to the spinner
        binding.mySpinner.adapter = adapter
    }
}

Keep in mind that the list is a RecyclerView . It means, essentially, that if you change one of the items color, you gotta change the color to all of the other items also (if the position == 0 AND if the position,= 0, cause the position == 0 item will be used again and again)

 override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, id: Long) {
            if (position == 0) {
                (view as TextView).setTextColor(resources.getColor(R.color.colorgray))
            }

try to set position of zero on itemSelected overriden function..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM