John Kiran
John Kiran

Reputation: 79

How to change TextSize and Background Color of a spinner

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.

Upvotes: 3

Views: 415

Answers (2)

Samrat Dutta
Samrat Dutta

Reputation: 1737

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.

Upvotes: 2

Antrromet
Antrromet

Reputation: 15424

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);

Upvotes: 1

Related Questions