3

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.

John Kiran
  • 79
  • 1
  • 14
  • 1
    check this answer http://stackoverflow.com/questions/9476665/how-to-change-spinner-text-size-and-text-color – Nishant Jul 18 '15 at 17:13

2 Answers2

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
  • may i know, where you changed Color and Size ? – John Kiran Jul 18 '15 at 17:42
  • <![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
  • Because we can post another question only after 90 mins gap – John Kiran Jul 18 '15 at 18:55
  • 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
  • Thank you, You are Genius @Samrat Dutta – John Kiran Jul 18 '15 at 19:32
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