Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Change the Text Size for a Spinner

Author: Daniel Fowler
Published? false
FormatLanguage: WikiFormat

Problem:

A Spinner is not like a TextView, the textSize attribute will not work (a Spinner is a ViewGroup).

Solution:

A Spinner contains other Views, style the contained Views to change the textSize and any other text attributes.

Discussion:

In Drop-down Chooser via the Spinner Class it was shown how to set up a Spinner and access the selected item. What if the text attributes for the selected item and list of items needed to be displayed differently to fit in with the look of an App. Trying to use an attribute to change the textSize will not work:

<Spinner android:id="@+id/contextChooser"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:prompt="@string/context_choice"
  android:textSize="22sp"/>

Spinner is a ViewGroup which is a container for other Views, in this case the View for the closed state and optionally the View for the dropped down state. As seen in Drop-down Chooser via the Spinner Class an Adapter is created and given a View when the static method createFromResource(...) is called. This Adapter is assigned to the Spinner, which then uses the View attached to the Adapter. Optionally a different View can be used for the drop down using the setDropDownViewResource(...) Adapter method. One solution to change the textSize, and other text attributes, is to pass in a custom layout instead of using an Android default layout (android.R.layout.simple_spinner_item was used in the other recipe). In this example another Android XML file is created in the res/layout folder called my_spinner.xml, the code for a TextView is added:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/text1"
  android:singleLine="true"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="22sp"/>

Pass the new layout to the ArrayAdapter creation (the id has to be text1 for the Adapter to use the View):

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.context_names, R.layout.my_spinner);

The text in the Spinner is now larger, but the default styling has been lost. This could be solved by assigning colors to the TextView but this would remove the Spinner styling from any theme that is being used on the device. Fortunately by using the Android feature that allows for the styling on Views to inherit from existing styles it is possible to get the Spinner back to the correct look and with the text size increase. Here a new XML file is created in the res/values folder, e.g. my_style.xml. In the file a style element is defined, here named MySpinnerLook. The style will inherit from an existing Android style. These can be viewed in platforms/android-X/data/res/values/styles.xml where the android-sdk folder resides, with X being the API level of the platform being used. In this file is found the Widget.TextView.SpinnerItem style. This is assigned to the parent attribute of the MySpinnerLook style (prefixing with @android:style/ or just @android:). The textSize attribute is moved from the TextView layout file into this new style file. The my_style.xml file will have this code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MySpinnerLook" parent="@android:TextAppearance.Widget.TextView.SpinnerItem">
    <item name="android:textSize">22sp</item>
  </style>
</resources>

The my_spinner.xml file links to the style file instead of having the TextSize attribute:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/text1"
  android:singleLine="true"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  style="@style/MySpinnerLook"/>

In summary to change the text size for a Spinner either:

  • Create a custom TextView layout.
  • Change the text size with the android:textSize attribute.
  • Change the text color with android:textColor in the new style file.

Or:

  • Create a custom style.
  • Use android:TextAppearance.Widget.TextView.SpinnerItem as the parent style.
  • Change the text size with the android:textSize attribute.

See Also:

Drop-down Chooser via the Spinner Class

mpa1 2016-03-18 21:36:50.106 As an unsophisticated, but avid android developer, I have read many descriptions and discussions about how to change the text size on a Spinner. They were incomprehensible! Even though I don't fully understand all the interactions described here, this is the only explanation that has enabled me accomplish what I needed to do. Thank you, Daniel.