I'm creating a spinner in my layout xml files and setting an string array to this spinner. If I change the textstyle of the spinner the text is not affected by the changes.
I read in the googlegroups that a spinner has no text and therefore the textstyle can not be changed and I have to change the style of the textview that is shown in the spinner. But how can I do that. Preferably in my xml file.
If you don't want such workarounds, there's a simple way. Get the textview from the spinner and change its parameters:
if you want just change background of popup View in spinner, call this method:
Via XML Only
As a follow-up to @Cyril REAL's excellent answer, here is a thorough implementation of how to style your Spinners just through XML if you're populating your Spinner via
android:entries
.The above answers work if you're creating your Spinner via code but if you're setting your Spinner entries via XML, i.e. using
android:entries
, then you can adjust the text size and other attributes with the following two theme settings:In your res/values/styles.xml
Actually you can customize spinner's text by xml.
In your own style, define a :
and define this style also :
When you instantiate the spinner's adapter in the java code, you can use the default android.R.layout.simple_spinner_item
As my predecessor specified, you can't do it on the main XML layout file where the Spinner component is.
And the answer above is nice, but if we want to use Google's best practices, like you know... to use styles for everything... you could do it in 3 'easy' steps as follows:
Step 1: You need an extra file under your layout folder with the look for the Spinner's items:
Name this file: spinner_item_text.xml
Step 2: Then, on your Activity Class when you are filling the Spinner with an array of items:
Note that the R.layout.spinner_item_text resource is in your own R's file.
Step 3: Under your values folder, create or use (you might have one already) the file styles.xml. The style entry needed should look like this one:
And that's it!
So far it has been really, really handy to put all about text sizes, styles, colors, etc... on a styles.xml file so it's easy to maintain.
When you create the Adapter that backs the Spinner you can set a layout for the spinner item.
You can style some_text_view the way you want.