In Android development, there is what we call a Spinner. Android Spinners allow you to display several options from which one value can be selected by the user. You can customize the values — change the text size and color. However, some beginners may find it hard to change Android Spinner text size. Although there have been forums discussing this problem, this remains a challenge for a number of Android developers. So, this article will reveal two sure ways to customize the text size of your Android Spinner.
What is Android Spinner?
Before jumping into the steps in changing the text size in Spinner, it’s important to get to know the Android Spinner itself? Is it a useful feature in Android development?
Android Spinner is a great tool to display several options to users from which he/she can choose one. It’s a dropdown menu with multiple values and the end-user can select only one value. You might not aware of that, but perhaps, some of your favorite apps used Android Spinner. Apps with a dropdown menu have tendencies to implement Android Spinner in the backend.
Ways to Change Android Spinner Text Size
The Spinner in the layout file (your .xml file) doesn’t define the View that the value being displayed uses. It’ll only be defined once the
ArrayAdapter is created when the data is linked to the Spinner. Through this, some commands for the data value, including text size and style can be implemented.
Make a Custom Layout
Try making a custom XML file for your Spinner item. In this layout file, you can use the
TextView user interface control. You can define the textSize XML attribute to the size of the text you want to be followed by ‘
sp.’ You can refer to the following code to learn more.
There are different units when it comes to Android development. If you have noticed, you can find px, dp, sp, etc. For text sizes, it’s recommended to use sp. SP means scalable pixels or scale-independent pixels. No need of setting up your res file because you can just call your Spinner layout file to show the items on your Spinner.
Alternatively, you can also do a custom layout by creating an XML file in the res/layout folder. Go to File>New, then Layout resource file. Set the root element to
TextView and you are good to go. Set the id to
text1, for example, then, set the
textSize to your liking.
Now, you can pass the new layout to the
ArrayAdapter. Ensure that the id you will use is the same as what you define in the layout file. The only downside of having a custom layout is that the default styling will be lost. If you don’t like that, you can extend the default theme.
Extend Default Theme
To increase the text size of the dropdown values without altering the style, simply extend a default theme. Go to the res/values folder, create a new Values resource file, and give it a go. Set the Source to
main and the directory name to
values — the rest as default and hit the OK button.
The new style element will be defined (let’s say
NewLook), but it’ll inherit an existing Android-style. Therefore, you can’t see it on the project’s localized styles.xml, rather, on Android’s style.xml file, itself. The
Widget.TextView.SpinnerItem style is now assigned to the parent attribute. The
textSize attribute can’t be used anymore — it becomes an item of the new style attribute. You can now point to the newly created style file via the
style attribute. See the difference below.
Why android:textSize on Spinner Doesn’t Work?
Even though the
android:textSize was mentioned on the list, Spinner itself doesn’t have a
textSize attribute. This attribute can only be used with
TextView. Therefore, you can’t use the attributes
android:textApperance on Spinner.
The Final Say
Android Spinner is a simple and easy way to present multiple options to front-end users, from which, they can choose one input. Spinners are like dropdowns where you can present multiple values from which the end-user can select a value. Therefore, if you are using several dropdown items, it’s important to do customization — change text size or color, perhaps. One common difficulty neophyte Android developers have been on the Android Spinner text size customization.
If you happen to use Android Spinner, but the
android:textSize doesn’t give you anything, it’s because it won’t work with Spinner. This article outlines two sure ways to customize Android Spinner. One way is to create a custom layout for your Spinner. Or, you can extend the default theme to accommodate more customization. Either of the two allows you to change the text size values for your Spinner. If you have known of any other ways to do this, don’t hesitate to leave a comment below.
I am a Software developer with approx 4 years of experience in building various responsive and beautiful websites and apps.
Language: Ruby, Java, HTML/CSS, Android
Frameworks: Ruby on Rails, Hosting: Heroku, Godaddy
Database: Mysql, Postgres, Mongo, Oracle
Also please look on my stack overflow profile.