How to Change TextSize and Background Color of the Spinner

I want to change the background color and text color for each element in the Spinner, I have already gone through several answers to questions similar to mine, but have not received the response weather that we can change. textSize / Background. Spinner color or not? If anyone has a simple answer, I would appreciate their answer. Thanks in advance.

+3


source to share


2 answers


strings.xml

Create a tag in the file <string-array name="spinner">

. create tags <item>

inside each post . Wrap the text in each item

tag <![CDATA[ ...raw html... ]]>

. Paste the formatting information into the raw html portion of the course.

In action

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 out the top answer from this post:

Set TextView text from html formatted string resource to XML

While this applies to TextView

, it can be modified for use in Spinner

too.

+2


source


You will need 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 this file in your code like this

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item_spinner, mListData);

      

+1


source







All Articles