Change text color of selected item in spinner

后端 未结 14 1525
情深已故
情深已故 2020-11-29 06:17

How can I change the font color of the selected item in a spinner?

I am able to change the background color of the selected item, the color of the dropdown item etc,

相关标签:
14条回答
  • 2020-11-29 06:57

    just use this line onItemSelected listner -

    public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,long arg3) 
         {
    
           item = (String) parent.getItemAtPosition(arg2);
    
    
           ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
    
     }
    
    0 讨论(0)
  • 2020-11-29 07:02

    You can change the selected text color by adding OnItemSelectedListener to the spinner

    qtySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) view).setTextColor(Color.BLACK); //Change selected text color
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    });
    
    0 讨论(0)
  • 2020-11-29 07:02

    If your spinner is using an ArrayAdapter, you can simply pass a custom layout for your spinner items. That layout can be a simple textView but with the android:textColor attribute.

    MainActivity.java onCreate()

    adapter = new ArrayAdapter<>(this, R.layout.spinner_custom_textcolor, data);
    spinner.setAdapter(adapter);
    

    spinner_custom_textcolor.xml

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle" 
    android:textColor="@color/YOUR_COLOR_HERE"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>
    

    *Everything from the above layout except for android:textColor is a direct copy from android.R.layout.simple_spinner_item

    0 讨论(0)
  • 2020-11-29 07:04

    some of you that using MaterialBetterSpinner and Binding your Layouts, all the above won't help, try this, hope it helps you:

    public class MyAdapter extends ArrayAdapter<String> {      
    
            public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
                super(context, textViewResourceId, objects);           
    
            }
    
            @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 = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false);
                rowBinding.tv1.setText(mMy.getValues().get(position));
                if(position == mMy.getCurrentIndex()) {
                    rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font
                    rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color
                }
                return rowBinding.getRoot();
            }
        }
    

    yourXML is something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <layout>
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@color/colorBackgroundStart">
            <TextView
                android:id="@+id/tv1"
                android:layout_width="0dp"
                android:layout_weight="0.7"
                android:layout_height="30dp"
                android:textColor="#fff"
                android:textSize="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="8dp"/>
    
    </layout>
    

    create a spinner with this adapter and yourXML :

    final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());
    
    final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
    spinner.setAdapter(adapter);
    
    0 讨论(0)
  • 2020-11-29 07:06

    Setting ((TextView) view).setTextColor(getResources().getColor(R.color.black)); inside onItemSelected(...) listener method will work. It sets color for your selected spinner text.

    0 讨论(0)
  • 2020-11-29 07:06

    The solution that worked for me was to use a CheckedTextView for the the drop down resource view and then change the color of the checked item using a color selector.

    spinner_dropdown_item.xml in the layout folder:

    <?xml version="1.0" encoding="utf-8"?>
    
    <!-- A `CheckedTextView` allows the color of the text to be changed when it is selected (checked). -->
    <CheckedTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/spinner_item_textview"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:maxLines="1"
        android:ellipsize="end"
        android:paddingStart="20dp"
        android:paddingEnd="20dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:textSize="18sp"
        android:textColor="@color/spinner_color_selector"
        android:background="@color/spinner_background" />
    

    spinner_color_selector in the color folder:

    <?xml version="1.0" encoding="utf-8"?>
    
    <!-- Highlight the selected (checked) item when the spinner is open. -->
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_checked="true" android:color="@color/white" />
        <item android:color="@color/blue_100" />
    </selector>
    

    spinner_dropdown_item.xml must be set as the DropDownResourceView for the Adapter. In my case I am using a ResourceArrayAdapter that pulls information from multiple sources.

    // Setup a `MatrixCursor` for the static entries.
    String[] matrixCursorColumnNames = {DatabaseHelper._ID, DatabaseHelper.NAME};
    MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
    matrixCursor.addRow(new Object[]{-2, getString(R.string.first_spinner_item)});
    matrixCursor.addRow(new Object[]{-1, getString(R.string.second_spinner_item)});
    
    // Get a `Cursor` with the list of additional items from the database.
    Cursor cursor = DatabaseHelper.getCursor();
    
    // Combine `matrixCursor` and `cursor`.
    MergeCursor mergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
    
    // Create a `ResourceCursorAdapter` for the spinner with `this` context.  `0` specifies no flags.;
    ResourceCursorAdapter resourceCursorAdapter = new ResourceCursorAdapter(this, R.layout.spinner_item, mergeCursor, 0) {
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // Get a handle for the spinner item `TextView`.
            TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview);
    
            // Set the `TextView` to display the name.
            spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NAME)));
        }
    };
    
    // Set the `ResourceCursorAdapter` drop drown view resource.
    resourceCursorAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    
    // Get a handle for the `Spinner`.
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    
    // Set the adapter for the folder `Spinner`.
    spinner.setAdapter(resourceCursorAdapter);
    

    Because ResourceCursorAdapter uses the same bindView to populate the spinner when it is open and closed, the id of the TextView in spinner_dropdown_item.xml and spinner_item.xml must be the same.

    spinner_item.xml in the layout folder:

    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/spinner_item_textview"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:maxLines="1"
        android:ellipsize="end"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:textSize="18sp"
        android:textColor="@color/primaryTextColor" />
    
    0 讨论(0)
提交回复
热议问题