Related
In my Android application, I am using spinner, and I have loaded data from the SQLite database into the spinner, and it's working properly. Here is the code for that.
Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();
list.add("All Lists");
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
list.add(keyList[i]);
cursor.moveToNext();
}
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
Now I want to change the text color and text size of spinner data. I have used following XML lines to my spinner tag on my XML file, but it is not working.
android:textColor="#android:color/white"
android:textSize="11dp"
How can I change the text color and text size of my spinner?
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.
Simple and crisp...:
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
If all the spinners may have the same text color for their TextView items, another approach is to use a custom style for spinner dropdown items:
In res/values/styles.xml:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:spinnerDropDownItemStyle">#style/mySpinnerItemStyle</item>
</style>
<style name="mySpinnerItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">#color/my_spinner_text_color</item>
</style>
</resources>
And define your custom color in res/values/colors.xml:
<color name="my_spinner_text_color">#808080</color>
Here is a link that can help you to change the color of the Spinner:
Click here
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:textSize="20sp"
android:entries="#array/planets"/>
You need to create your own layout file with a custom definition for the spinner item spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />
If you want to customize the dropdown list items, you will need to create a new layout file. spinner_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
And finally another change in the declaration of the spinner:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
That's it.
If you work with android.support.v7.widget.AppCompatSpinner here is the simplest tested solution using styles:
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spefcialFx"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:theme="#style/Spinner"
android:entries="#array/special_fx_arrays"
android:textSize="#dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>
And the style:
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:backgroundTint">#color/red</item>
<item name="android:textSize">14sp</item>
</style>
The only downside is the android:backgroundTint sets color for both the dropdown arrow and the dropdown background.
To prevent lagging, you need to not only set the text properties in the onItemSelected listener, but also in the Activity's onCreate method (but it's a little tricky).
Specifically, you need to put this in onCreate after setting the adapter:
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
And then put this in onItemSelected:
((TextView) view).setTextColor(backgroundColor);
Here is a full example:
#Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
For more details, see my question.
If you want the text color to change in the selected item only, then this can be a possible workaround. It worked for me and should work for you as well.
spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE);
}
});
For someone who needs only Style way for AppCompat.
Result
styles.xml
<resources>
...
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">#color/material_grey_700</item>
<item name="android:textSize">12sp</item>
</style>
</resources>
your_spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/content_spinner"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:entries="#array/shipping_tracking_carrier_names"
android:spinnerMode="dropdown"
android:theme="#style/Spinner" />
<EditText
android:id="#+id/content_input"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:textColor="#color/material_grey_700"
android:textSize="12sp" />
...
</LinearLayout>
Plus
And if you want to set android:entries programmatically with defined style.
Try this.
AppCompatSpinner spinner = findViewById(R.id.content_spinner);
CharSequence[] entries = getResources().getTextArray(R.array.shipping_tracking_carrier_names);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
As in the code, using same Context with the Spinner is most important thing.
spinner.getContext()
we can change style of spinner textview set theme for spinner like this:
styles.xml:
<style name="mySpinnerItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textSize">#dimen/_11ssp</item>
<item name="android:textColor">#color/blue</item>
<item name=...</item>
</style>
then
<Spinner
android:theme="#style/mySpinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
if you want to change spinner textview attribute programtically:
Programatically:
val textView = (view.getChildAt(0) as TextView)
textView.setTextColor(resources.getColor(R.color.dark_mode))
For those who want to change DrowDownIcon color
you can use like this
spinner.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
To change the color of spinner text :
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}
If you want a simple method, in order to add items to a dropdown, you usually add them to the strings.xml. Here is an example on how to add colour by using the strings.xml file:
SELECT AGE RANGE
<string-array name="age_array">
<item> 0-6 </item> //No custom colour
<item><font fgcolor='#FF4CD964'> 12+ </font></item> //With custom colour
</string-array>
Can change the text colour by overriding the getView method as follows:
new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
//change the color to which ever you want
((CheckedTextView) view).setTextColor(Color.RED);
//change the size to which ever you want
((CheckedTextView) view).setTextSize(5);
//for using sp values use setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
return view;
}
}
Rather than making a custom layout to get a small size and if you want to use Android's internal small size LAYOUT for the spinner, you should use:
"android.R.layout.simple_gallery_item" instead of "android.R.layout.simple_spinner_item".
ArrayAdapter<CharSequence> madaptor = ArrayAdapter
.createFromResource(rootView.getContext(),
R.array.String_visitor,
android.R.layout.simple_gallery_item);
It can reduce the size of spinner's layout. It's just a simple trick.
If you want to reduce the size of a drop down list use this:
madaptor.setDropDownViewResource(android.R.layout.simple_gallery_item);
The easiest way to re-use/change the android.R.layout resources is just go the definition. In Android Studio, do Ctrl + B on android.R.layout.simple_spinner_item.xml.
It will take you to the resource file. Just copy the resource file and add a new layout in your Package.R.layout folder and change the textColor of textview as you like and then just call it in adapter like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String(Context,R.layout.spinner_item, spinnerlist);
Just wanted to make a small change on the correct answer at the top.
Make a custom XML file for your spinner item inside the layout directory.
spinner_style.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"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:textSize="15sp"
android:textColor="#FF0000"
android:padding="5dp"
/>
Now use this file to show your spinner items inside your java file:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.spinner_style,list);
adapter.setDropDownViewResource(R.layout.spinner_style);
Simplest: Works for me
TextView spinnerText = (TextView) spinner.getChildAt(0);
spinnerText.setTextColor(Color.RED);
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
just use this:
ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
R.layout.spinner_list_item, categories);
adapter_category
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Another variation of Ashraf's solution would be to make sure you're taking into account screen sizes. You'll need to get the spinner in onCreate and set the listener after you set the adapter:
//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);
Then you can start changing the text size of the view that's showing before the spinner is clicked:
private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
All you need to do is create layout specific folders like this:
values-sw360dp
values-sw600dp
values-sw800dp
an then add an xml file named "bool.xml" into each of those folders:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
<bool name="isLargeTablet">false</bool>
</resources>
First we have to create the simple xml resource file for the textview like as below:
<?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"
/>
and save it. after set on your adapterlist.
you can have this type of adapter for spinner, totally customized:
ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#676767"));
((TextView) v).setTypeface(vrFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTypeface(vrFont);
((TextView) v).setTextColor(Color.parseColor("#676767"));
if (position == 0) {
((TextView) v).setTextColor(Color.parseColor("#979797"));
}
return v;
}
while R.layout.spinner_text is:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical|left"
android:ellipsize="marquee"
android:maxLines="1"
android:textColor="#color/whiteThree" />
Try this method. It is working for me.
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView = (TextView) view;
((TextView) adapterView.getChildAt(0)).setTextColor(Color.RED);
((TextView) adapterView.getChildAt(0)).setTextSize(20);
Toast.makeText(this, textView.getText()+" Selected", Toast.LENGTH_SHORT).show();
}
String typeroutes[] = {"Select","Direct","Non Stop"};
Spinner typeroute;
typeroute = view.findViewById(R.id.typeroute);
final ArrayAdapter<String> arrayAdapter5 = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, typeroutes) {
#Override
public boolean isEnabled(int position) {
if (position == 0) {
// Disable the first item from Spinner
// First item will be use for hint
return false;
} else {
return true;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#ffffff"));
return v;
} ---->in this line very important so add this
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
// Set the hint text color gray
tv.setTextColor(Color.GRAY);
} else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
arrayAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeroute.setAdapter(arrayAdapter5);
that's all enjoy your coding...
I have done this as following.I have use getDropDownView() and getView() methods.
Use getDropDownView() for opened Spinner.
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
if (!((LabelItem) getItem(position)).isEnabled()) {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
} else {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item));
}
return view;
}
And Use getView() for closed Spinner.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
return view;
}
just add new style like this:
<style name="mySpinnerItemStyle" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColor">#000</item>
<item name="android:color">#000</item>
</style>
and use it:
<Spinner
android:id="#+id/spinnerCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/mySpinnerItemStyle"
android:layout_margin="5dp" />
In my Android application, I am using spinner, and I have loaded data from the SQLite database into the spinner, and it's working properly. Here is the code for that.
Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();
list.add("All Lists");
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
list.add(keyList[i]);
cursor.moveToNext();
}
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
Now I want to change the text color and text size of spinner data. I have used following XML lines to my spinner tag on my XML file, but it is not working.
android:textColor="#android:color/white"
android:textSize="11dp"
How can I change the text color and text size of my spinner?
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.
Simple and crisp...:
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
If all the spinners may have the same text color for their TextView items, another approach is to use a custom style for spinner dropdown items:
In res/values/styles.xml:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:spinnerDropDownItemStyle">#style/mySpinnerItemStyle</item>
</style>
<style name="mySpinnerItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">#color/my_spinner_text_color</item>
</style>
</resources>
And define your custom color in res/values/colors.xml:
<color name="my_spinner_text_color">#808080</color>
Here is a link that can help you to change the color of the Spinner:
Click here
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:textSize="20sp"
android:entries="#array/planets"/>
You need to create your own layout file with a custom definition for the spinner item spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />
If you want to customize the dropdown list items, you will need to create a new layout file. spinner_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
And finally another change in the declaration of the spinner:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
That's it.
If you work with android.support.v7.widget.AppCompatSpinner here is the simplest tested solution using styles:
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spefcialFx"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:theme="#style/Spinner"
android:entries="#array/special_fx_arrays"
android:textSize="#dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>
And the style:
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:backgroundTint">#color/red</item>
<item name="android:textSize">14sp</item>
</style>
The only downside is the android:backgroundTint sets color for both the dropdown arrow and the dropdown background.
To prevent lagging, you need to not only set the text properties in the onItemSelected listener, but also in the Activity's onCreate method (but it's a little tricky).
Specifically, you need to put this in onCreate after setting the adapter:
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
And then put this in onItemSelected:
((TextView) view).setTextColor(backgroundColor);
Here is a full example:
#Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
For more details, see my question.
If you want the text color to change in the selected item only, then this can be a possible workaround. It worked for me and should work for you as well.
spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE);
}
});
For someone who needs only Style way for AppCompat.
Result
styles.xml
<resources>
...
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">#color/material_grey_700</item>
<item name="android:textSize">12sp</item>
</style>
</resources>
your_spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/content_spinner"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:entries="#array/shipping_tracking_carrier_names"
android:spinnerMode="dropdown"
android:theme="#style/Spinner" />
<EditText
android:id="#+id/content_input"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:textColor="#color/material_grey_700"
android:textSize="12sp" />
...
</LinearLayout>
Plus
And if you want to set android:entries programmatically with defined style.
Try this.
AppCompatSpinner spinner = findViewById(R.id.content_spinner);
CharSequence[] entries = getResources().getTextArray(R.array.shipping_tracking_carrier_names);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
As in the code, using same Context with the Spinner is most important thing.
spinner.getContext()
we can change style of spinner textview set theme for spinner like this:
styles.xml:
<style name="mySpinnerItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textSize">#dimen/_11ssp</item>
<item name="android:textColor">#color/blue</item>
<item name=...</item>
</style>
then
<Spinner
android:theme="#style/mySpinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
if you want to change spinner textview attribute programtically:
Programatically:
val textView = (view.getChildAt(0) as TextView)
textView.setTextColor(resources.getColor(R.color.dark_mode))
For those who want to change DrowDownIcon color
you can use like this
spinner.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
To change the color of spinner text :
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}
If you want a simple method, in order to add items to a dropdown, you usually add them to the strings.xml. Here is an example on how to add colour by using the strings.xml file:
SELECT AGE RANGE
<string-array name="age_array">
<item> 0-6 </item> //No custom colour
<item><font fgcolor='#FF4CD964'> 12+ </font></item> //With custom colour
</string-array>
Can change the text colour by overriding the getView method as follows:
new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
//change the color to which ever you want
((CheckedTextView) view).setTextColor(Color.RED);
//change the size to which ever you want
((CheckedTextView) view).setTextSize(5);
//for using sp values use setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
return view;
}
}
Rather than making a custom layout to get a small size and if you want to use Android's internal small size LAYOUT for the spinner, you should use:
"android.R.layout.simple_gallery_item" instead of "android.R.layout.simple_spinner_item".
ArrayAdapter<CharSequence> madaptor = ArrayAdapter
.createFromResource(rootView.getContext(),
R.array.String_visitor,
android.R.layout.simple_gallery_item);
It can reduce the size of spinner's layout. It's just a simple trick.
If you want to reduce the size of a drop down list use this:
madaptor.setDropDownViewResource(android.R.layout.simple_gallery_item);
The easiest way to re-use/change the android.R.layout resources is just go the definition. In Android Studio, do Ctrl + B on android.R.layout.simple_spinner_item.xml.
It will take you to the resource file. Just copy the resource file and add a new layout in your Package.R.layout folder and change the textColor of textview as you like and then just call it in adapter like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String(Context,R.layout.spinner_item, spinnerlist);
Just wanted to make a small change on the correct answer at the top.
Make a custom XML file for your spinner item inside the layout directory.
spinner_style.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"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:textSize="15sp"
android:textColor="#FF0000"
android:padding="5dp"
/>
Now use this file to show your spinner items inside your java file:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.spinner_style,list);
adapter.setDropDownViewResource(R.layout.spinner_style);
Simplest: Works for me
TextView spinnerText = (TextView) spinner.getChildAt(0);
spinnerText.setTextColor(Color.RED);
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
just use this:
ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
R.layout.spinner_list_item, categories);
adapter_category
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Another variation of Ashraf's solution would be to make sure you're taking into account screen sizes. You'll need to get the spinner in onCreate and set the listener after you set the adapter:
//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);
Then you can start changing the text size of the view that's showing before the spinner is clicked:
private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
All you need to do is create layout specific folders like this:
values-sw360dp
values-sw600dp
values-sw800dp
an then add an xml file named "bool.xml" into each of those folders:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
<bool name="isLargeTablet">false</bool>
</resources>
First we have to create the simple xml resource file for the textview like as below:
<?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"
/>
and save it. after set on your adapterlist.
you can have this type of adapter for spinner, totally customized:
ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#676767"));
((TextView) v).setTypeface(vrFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTypeface(vrFont);
((TextView) v).setTextColor(Color.parseColor("#676767"));
if (position == 0) {
((TextView) v).setTextColor(Color.parseColor("#979797"));
}
return v;
}
while R.layout.spinner_text is:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical|left"
android:ellipsize="marquee"
android:maxLines="1"
android:textColor="#color/whiteThree" />
Try this method. It is working for me.
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView = (TextView) view;
((TextView) adapterView.getChildAt(0)).setTextColor(Color.RED);
((TextView) adapterView.getChildAt(0)).setTextSize(20);
Toast.makeText(this, textView.getText()+" Selected", Toast.LENGTH_SHORT).show();
}
String typeroutes[] = {"Select","Direct","Non Stop"};
Spinner typeroute;
typeroute = view.findViewById(R.id.typeroute);
final ArrayAdapter<String> arrayAdapter5 = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, typeroutes) {
#Override
public boolean isEnabled(int position) {
if (position == 0) {
// Disable the first item from Spinner
// First item will be use for hint
return false;
} else {
return true;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#ffffff"));
return v;
} ---->in this line very important so add this
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
// Set the hint text color gray
tv.setTextColor(Color.GRAY);
} else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
arrayAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeroute.setAdapter(arrayAdapter5);
that's all enjoy your coding...
I have done this as following.I have use getDropDownView() and getView() methods.
Use getDropDownView() for opened Spinner.
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
if (!((LabelItem) getItem(position)).isEnabled()) {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
} else {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item));
}
return view;
}
And Use getView() for closed Spinner.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
return view;
}
just add new style like this:
<style name="mySpinnerItemStyle" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColor">#000</item>
<item name="android:color">#000</item>
</style>
and use it:
<Spinner
android:id="#+id/spinnerCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/mySpinnerItemStyle"
android:layout_margin="5dp" />
I am trying to set a custom selector to a ListView. It's working fine on newer devices but not working on lower version of devices.I want the ListView selected item to be stay highlighted.
Please help.
Thanks in advance.
ListView.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:orientation="vertical" >
<ListView
android:id="#+id/listViewBell"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector_color" >
</ListView>
</LinearLayout>
list_selectror_color.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/list_selector" />
<stroke
android:dashWidth="2dp"
android:width="1dp"
android:color="#color/white" />
</shape>
I have also tried with selector but nothing happens
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_selector_color" android:state_pressed="true"/>
<item android:drawable="#drawable/list_selector_color" android:state_focused="true"/>
<item android:drawable="#drawable/list_selector_color" android:state_selected="true"/>
<item android:drawable="#drawable/list_selector_normal"/>
</selector>
Here is my Custom adapter getView Method
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_bell_items, parent,
false);
ImageView imageview = (ImageView) convertView
.findViewById(R.id.list_bell_image);
imageview.setImageResource(mDataImage[position]);
TextView textview = (TextView) convertView
.findViewById(R.id.txt_bell_title);
textview.setText(mDataText[position]);
return convertView;
}
I had similar problem. Sorry, cannot comment, so I post a possible answer.
Remove android:listSelector="#drawable/list_selector_color" property from your ListView declaration
In your R.layout.listview_bell_items specify your custom selector for a root layout. E.g., if root layout of your list item is RelativeLayout, try:
<RelativeLayout ... android:background="#drawable/listitem_selector">...
The same goes for any other type of Layout.
If this still still does not give you the result you want, provide more details.
Update
Ok, if nothing else helps, there's a temporary dirty workaround of your problem. I do not see why it would not work.
Introduce a selectedPos variable holding currently selected item.
private class MyAdapter extends .../*your base adapter*/ {
private static final int NOT_SELECTED = -1;
private int selectedPos = NOT_SELECTED;
// if called with the same position multiple lines it works as toggle
public void setSelection(int position) {
if (selectedPos == position) {
selectedPos = NOT_SELECTED;
} else {
selectedPos = position;
}
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == selectedPos) {
// your color for selected item
view.setBackgroundColor(Color.parseColor("#000000"));
} else {
// your color for non-selected item
view.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
return view;
}
}
Now, add the following code after you create and set ListView's adapter:
final MyAdapter adapter = new MyAdapter(...);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.setSelection(position);
}
});
That is what I found out:
To get the list selector working the are two approaches:
1)
You use the OnItemClickListener.
Then the list selector drawable/color will work as expected when set it on the list view.
Then you may use a TouchListener for getting ClickEvents of any child view instead of using a ClickListener.
2)
You have set ClickListener on any child of the row view.
In this case the list selector will not work when been set on the list view, so you have to set your list selector drawable/color as background of the row view.
I have been unable to set the color on the spinner widget. How is this styled?
Try using this adapter for your spinner:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(Home.Home_Group, R.layout.my_spinner_style, yourstringarray)
{
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(16);
((TextView) v).setTextColor(
getResources().getColorStateList(R.color.white)
);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
v.setBackgroundResource(R.drawable.spinner_bg);
((TextView) v).setTextColor(
getResources().getColorStateList(R.color.spinner_text)
);
((TextView) v).setTypeface(fontStyle);
((TextView) v).setGravity(Gravity.CENTER);
return v;
}
};
Add this xml to your layout,
my_spinner_style.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
And at last,
spinner.setAdapter(adapter);
Simple and crisp ....
private OnItemSelectedListener your_spinner _name= new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
The simplest form is:
m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);
If you want to change the text color :
Spinner Widgets Text Color
Else, making your own layout is the best way like JoxTraex said.
A shorter alternative to Andro'd answer is to let the ArrayAdapter create the item views for you from a layout resource:
final List<String> values = [SPINNER VALUES];
final ArrayAdapter<String> adapter = new ArrayAdapter<>(
activity, R.layout.my_spinner_item, values);
adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
spinner.setAdapter(adapter);
Then style your text to suit your needs in my_spinner_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="#style/my_spinner_item_style"
/>
Note: my_spinner_dropdown_item is used when the list of choices appear
For more information read the Spinners documentation.
Try understanding that you are using the dropdown list as provided by the defaults that are available in the SDK.
SIMPLY make your own layout with a custom adapter.
In my Android application, I am using spinner, and I have loaded data from the SQLite database into the spinner, and it's working properly. Here is the code for that.
Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();
list.add("All Lists");
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
list.add(keyList[i]);
cursor.moveToNext();
}
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
Now I want to change the text color and text size of spinner data. I have used following XML lines to my spinner tag on my XML file, but it is not working.
android:textColor="#android:color/white"
android:textSize="11dp"
How can I change the text color and text size of my spinner?
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.
Simple and crisp...:
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
If all the spinners may have the same text color for their TextView items, another approach is to use a custom style for spinner dropdown items:
In res/values/styles.xml:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:spinnerDropDownItemStyle">#style/mySpinnerItemStyle</item>
</style>
<style name="mySpinnerItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">#color/my_spinner_text_color</item>
</style>
</resources>
And define your custom color in res/values/colors.xml:
<color name="my_spinner_text_color">#808080</color>
Here is a link that can help you to change the color of the Spinner:
Click here
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:textSize="20sp"
android:entries="#array/planets"/>
You need to create your own layout file with a custom definition for the spinner item spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />
If you want to customize the dropdown list items, you will need to create a new layout file. spinner_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
And finally another change in the declaration of the spinner:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
That's it.
If you work with android.support.v7.widget.AppCompatSpinner here is the simplest tested solution using styles:
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spefcialFx"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:theme="#style/Spinner"
android:entries="#array/special_fx_arrays"
android:textSize="#dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>
And the style:
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:backgroundTint">#color/red</item>
<item name="android:textSize">14sp</item>
</style>
The only downside is the android:backgroundTint sets color for both the dropdown arrow and the dropdown background.
To prevent lagging, you need to not only set the text properties in the onItemSelected listener, but also in the Activity's onCreate method (but it's a little tricky).
Specifically, you need to put this in onCreate after setting the adapter:
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
And then put this in onItemSelected:
((TextView) view).setTextColor(backgroundColor);
Here is a full example:
#Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
For more details, see my question.
If you want the text color to change in the selected item only, then this can be a possible workaround. It worked for me and should work for you as well.
spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE);
}
});
For someone who needs only Style way for AppCompat.
Result
styles.xml
<resources>
...
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">#color/material_grey_700</item>
<item name="android:textSize">12sp</item>
</style>
</resources>
your_spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/content_spinner"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:entries="#array/shipping_tracking_carrier_names"
android:spinnerMode="dropdown"
android:theme="#style/Spinner" />
<EditText
android:id="#+id/content_input"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:textColor="#color/material_grey_700"
android:textSize="12sp" />
...
</LinearLayout>
Plus
And if you want to set android:entries programmatically with defined style.
Try this.
AppCompatSpinner spinner = findViewById(R.id.content_spinner);
CharSequence[] entries = getResources().getTextArray(R.array.shipping_tracking_carrier_names);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
As in the code, using same Context with the Spinner is most important thing.
spinner.getContext()
we can change style of spinner textview set theme for spinner like this:
styles.xml:
<style name="mySpinnerItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textSize">#dimen/_11ssp</item>
<item name="android:textColor">#color/blue</item>
<item name=...</item>
</style>
then
<Spinner
android:theme="#style/mySpinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
if you want to change spinner textview attribute programtically:
Programatically:
val textView = (view.getChildAt(0) as TextView)
textView.setTextColor(resources.getColor(R.color.dark_mode))
For those who want to change DrowDownIcon color
you can use like this
spinner.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
To change the color of spinner text :
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}
If you want a simple method, in order to add items to a dropdown, you usually add them to the strings.xml. Here is an example on how to add colour by using the strings.xml file:
SELECT AGE RANGE
<string-array name="age_array">
<item> 0-6 </item> //No custom colour
<item><font fgcolor='#FF4CD964'> 12+ </font></item> //With custom colour
</string-array>
Can change the text colour by overriding the getView method as follows:
new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
//change the color to which ever you want
((CheckedTextView) view).setTextColor(Color.RED);
//change the size to which ever you want
((CheckedTextView) view).setTextSize(5);
//for using sp values use setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
return view;
}
}
Rather than making a custom layout to get a small size and if you want to use Android's internal small size LAYOUT for the spinner, you should use:
"android.R.layout.simple_gallery_item" instead of "android.R.layout.simple_spinner_item".
ArrayAdapter<CharSequence> madaptor = ArrayAdapter
.createFromResource(rootView.getContext(),
R.array.String_visitor,
android.R.layout.simple_gallery_item);
It can reduce the size of spinner's layout. It's just a simple trick.
If you want to reduce the size of a drop down list use this:
madaptor.setDropDownViewResource(android.R.layout.simple_gallery_item);
The easiest way to re-use/change the android.R.layout resources is just go the definition. In Android Studio, do Ctrl + B on android.R.layout.simple_spinner_item.xml.
It will take you to the resource file. Just copy the resource file and add a new layout in your Package.R.layout folder and change the textColor of textview as you like and then just call it in adapter like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String(Context,R.layout.spinner_item, spinnerlist);
Just wanted to make a small change on the correct answer at the top.
Make a custom XML file for your spinner item inside the layout directory.
spinner_style.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"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:textSize="15sp"
android:textColor="#FF0000"
android:padding="5dp"
/>
Now use this file to show your spinner items inside your java file:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.spinner_style,list);
adapter.setDropDownViewResource(R.layout.spinner_style);
Simplest: Works for me
TextView spinnerText = (TextView) spinner.getChildAt(0);
spinnerText.setTextColor(Color.RED);
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
just use this:
ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
R.layout.spinner_list_item, categories);
adapter_category
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Another variation of Ashraf's solution would be to make sure you're taking into account screen sizes. You'll need to get the spinner in onCreate and set the listener after you set the adapter:
//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);
Then you can start changing the text size of the view that's showing before the spinner is clicked:
private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
All you need to do is create layout specific folders like this:
values-sw360dp
values-sw600dp
values-sw800dp
an then add an xml file named "bool.xml" into each of those folders:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
<bool name="isLargeTablet">false</bool>
</resources>
First we have to create the simple xml resource file for the textview like as below:
<?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"
/>
and save it. after set on your adapterlist.
you can have this type of adapter for spinner, totally customized:
ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#676767"));
((TextView) v).setTypeface(vrFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTypeface(vrFont);
((TextView) v).setTextColor(Color.parseColor("#676767"));
if (position == 0) {
((TextView) v).setTextColor(Color.parseColor("#979797"));
}
return v;
}
while R.layout.spinner_text is:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical|left"
android:ellipsize="marquee"
android:maxLines="1"
android:textColor="#color/whiteThree" />
Try this method. It is working for me.
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView = (TextView) view;
((TextView) adapterView.getChildAt(0)).setTextColor(Color.RED);
((TextView) adapterView.getChildAt(0)).setTextSize(20);
Toast.makeText(this, textView.getText()+" Selected", Toast.LENGTH_SHORT).show();
}
String typeroutes[] = {"Select","Direct","Non Stop"};
Spinner typeroute;
typeroute = view.findViewById(R.id.typeroute);
final ArrayAdapter<String> arrayAdapter5 = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, typeroutes) {
#Override
public boolean isEnabled(int position) {
if (position == 0) {
// Disable the first item from Spinner
// First item will be use for hint
return false;
} else {
return true;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#ffffff"));
return v;
} ---->in this line very important so add this
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
// Set the hint text color gray
tv.setTextColor(Color.GRAY);
} else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
arrayAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeroute.setAdapter(arrayAdapter5);
that's all enjoy your coding...
I have done this as following.I have use getDropDownView() and getView() methods.
Use getDropDownView() for opened Spinner.
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
if (!((LabelItem) getItem(position)).isEnabled()) {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
} else {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item));
}
return view;
}
And Use getView() for closed Spinner.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
return view;
}
just add new style like this:
<style name="mySpinnerItemStyle" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColor">#000</item>
<item name="android:color">#000</item>
</style>
and use it:
<Spinner
android:id="#+id/spinnerCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/mySpinnerItemStyle"
android:layout_margin="5dp" />