I want to change my spinner background color and text size of each item in the Spinner, i already have gone through few answers for questions similar to mine but did not get the answer weather we can change the textSize / Background Color of a spinner or not? If anyone have a simple answer, i will be thankful to their reply. Thanks in advance.
Asked
Active
Viewed 406 times
3
-
1check this answer http://stackoverflow.com/questions/9476665/how-to-change-spinner-text-size-and-text-color – Nishant Jul 18 '15 at 17:13
2 Answers
2
In the strings.xml
file create a <string-array name="spinner">
tag.
create <item>
tags inside with each entry.
Wrap the text in each item
with <![CDATA[ ...raw html... ]]>
tag. Put in the formatting information in the raw html part ofcourse.
In the activity,
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Check the top answer from this post :
Set TextView text from html-formatted string resource in XML
Though this refers to a TextView
, it can be modified to be used in a Spinner
too.

Community
- 1
- 1

Samrat Dutta
- 1,727
- 1
- 11
- 23
-
-
<![CDATA[... inside here, use raw html to change size, color...]]> Take a look at the answer in this post: [link](http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml) Click the link. – Samrat Dutta Jul 18 '15 at 17:45
-
Your solution is absolutely what i want, Thank you for you help @Samrat Dutta . AND after solving this my application encountered another problem i.e its getting force closed when i change my phones landscape mode from vertical to horizontal or vice-versa. i am using just a EditText component with attributes, android:width="fill_parent & android:height="fill_parent" Do you have any idea over my problem. If you have ay Idea please share here Thank you – John Kiran Jul 18 '15 at 18:48
-
First of all, you always use match_parent instead of fill_parent. Latest convention. But that is not causing the problem. Although you should use layout_weight="1" instead of android:height="fill_parent". Anyway, I need the Logcat error message when the application crashes to try and solve the issue. – Samrat Dutta Jul 18 '15 at 18:53
-
Why dont you post your problem as a separate question with the relevant code that you have tried? – Samrat Dutta Jul 18 '15 at 18:54
-
-
Please tell me the logcat error. That will help me understand the problem. – Samrat Dutta Jul 18 '15 at 18:56
-
07-19 00:26:31.116: E/AndroidRuntime(11780): FATAL EXCEPTION: main 07-19 00:26:31.116: E/AndroidRuntime(11780): Process: com.ex.trail, PID: 11780 07-19 00:26:31.116: E/AndroidRuntime(11780): java.lang.NullPointerException 07-19 00:26:31.116: E/AndroidRuntime(11780): at com.ex.trail.BibleActivity.onItemSelected(BibleActivity.java:42) 07-19 00:26:31.116: E/AndroidRuntime(11780): at android.widget.AdapterView.fireOnSelected(AdapterView.java:964) 07-19 00:26:31.116: E/AndroidRuntime(11780): at android.widget.AdapterView.selectionChanged(AdapterView.java:947) – John Kiran Jul 18 '15 at 19:01
-
07-19 00:26:31.116: E/AndroidRuntime(11780): at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1114) 07-19 00:26:31.116: E/AndroidRuntime(11780): at android.widget.AdapterView.handleDataChanged(AdapterView.java:1094) 07-19 00:26:31.116: E/AndroidRuntime(11780): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:180) 07-19 00:26:31.116: E/AndroidRuntime(11780): at android.widget.Spinner.onMeasure(Spinner.java:503) 07-19 00:26:31.116: E/AndroidRuntime(11780): at android.view.View.measure(View.java:17495) – John Kiran Jul 18 '15 at 19:02
-
It is a nullpointer Exception. The Activity cant find the EditText by id when rotated. It is very hard to answer without the code. If and when possible, do post a question with the code. Try putting `if(savedInstanceState != null) { return; }` inside the `onCreate()` method after `setContentView();` – Samrat Dutta Jul 18 '15 at 19:09
-
1
You'll have to create a custom xml list item for your spinner, something like this list_item_spinner.xml
<?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:background="#000000"
android:textColor="#FB26A9"
android:padding="10dip"/>
After that use that file in your code as follows
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item_spinner, mListData);

Antrromet
- 15,294
- 10
- 60
- 75
-
But i dont want to change color and size in TextView, i want same in Spinner – John Kiran Jul 18 '15 at 17:21
-
He wants each of his list items to have a different size and background. – Samrat Dutta Jul 18 '15 at 17:27
-
Yes you are correct @Samrat Dutta , If you have appropriate answer please forward it – John Kiran Jul 18 '15 at 17:33
-
I've already posted my solution below. If you have any confusion with it, feel free to ask in comments. – Samrat Dutta Jul 18 '15 at 17:34
-
1Did you take a look at http://stackoverflow.com/questions/5858294/android-each-item-of-spinner-different-color and – Antrromet Jul 18 '15 at 17:36