Android Change Text Color of Items in Spinner

How to change spinner text size and text color?

Make a custom XML file for your spinner item.

spinner_item.xml:

Give your customized color and size to text in this file.

<?xml version="1.0" encoding="utf-8"?>

<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:textColor="#FF0000"
android:padding="5dip"
/>

Now use this file to show your spinner items like:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);

You don't need to set the drop down resource. It will take spinner_item.xml only to show your items in spinner.

change Spinner text color

Try like this only for selected item color change:

spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
((TextView)parentView.getChildAt(0)).setTextColor(Color.RED);
}
});

Another way:

<style name="spinnerTheme">
<item name="android:textColor">@color/gray_dark</item>
</style>

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:theme="@style/spinnerTheme"/>

Change text color to selected item on Android Spinner

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
((TextView)parent.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF"));
}

public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});

How to change spinner text color

Here You have to set your spinner_item.xml in your array adapter. Add this piece of code in your .java file

Spinner yourSpinner;
ArrayAdapter<String> yourAdapter;
yourSpinner= (Spinner) findViewById(R.id.yourSpinnerID);
yourSpinner.setAdapter(yourAdapter);
yourAdapter= new ArrayAdapter<String>(this, R.layout.spinner_item, value);
//here value is your items in spinner..

How to change text color in spinner without using textview

I got solution for this, Create a new xml "spinner_style.xml"

<?xml version="1.0" encoding="UTF-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/Black" />

And fix this layout as your spinner style in your activity:

   ArrayAdapter<String > gender_adapter = new ArrayAdapter<String> (getActivity(), R.layout.spinner_style,gender_spinner );

android change text color of items in spinner

I figured out that to make this work you have to override the getDropDownView when setting up the ArrayAdapter in the main activity.

public class main extends Activity {  
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PatchedSpinner pSpinner = (PatchedSpinner) findViewById(R.id.spinner2);
ArrayList<String> testarray = new ArrayList<String>();
testarray.add("item0");
testarray.add("item1");
testarray.add("item2");
testarray.add("item3");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, testarray) {

@Override
public boolean isEnabled(int position) {
return position != 1;
}

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

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = this.getContext();
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}

TextView tv = (TextView) v.findViewById(R.id.spinnerTarget);
tv.setText(testarray.get(position));

switch (position) {
case 0:
tv.setTextColor(Color.RED);
break;
case 1:
tv.setTextColor(Color.BLUE);
break;
default:
tv.setTextColor(Color.BLACK);
break;
}
return v;
}
};

pSpinner.setAdapter(spinnerAdapter);
}

The layout that is being inflated is a custom layout called row.xml. it is used to set the display for the dropdown view.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14pt" />

Change text color of selected item in spinner

drawable/mybg:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true">
<color android:color="@color/black" />
</item>
</layer-list>

This will change the selected item color in the popup.



Related Topics



Leave a reply



Submit