I want to use custom font and color in Spinner. I can't modify them directly in <spinner/>
Here's my code
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5pt"
android:layout_marginTop="5pt"
android:backgroundTint="#d9d9d9"
android:entries="@array/dropdown_main" />
It should look like this:
Text font is Product Sans Bold, color is #333333
In summary to change the text size (or other style attributes) 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.
Use ArrayAdapter to store the courses list. Create a single MainActivity that contains the spinner and on clicking any item of spinner Toast with that course name will be shown. Creating the activities: There will be one activity and hence one XML file for MainActivity. activity_main.
A heads up before you get started: To add the font, you'll either want to set a minimum API version of 26 or include the Support Library v26.0 (for support starting at API version 16). This example shows how to use the support library; the only real difference is the <font-family>
using the app
or res-auto
namespace instead of android
.
You can keep the spinner as is but add a theme
value to your XML:
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5pt"
android:layout_marginTop="5pt"
android:backgroundTint="#d9d9d9"
android:entries="@array/dropdown_main"
android:theme="@style/SpinnerTheme" />
Your styles.xml
can contain the theme:
<style name="SpinnerTheme">
<item name="android:textColor">#333333</item>
<item name="android:fontFamily">@font/product_sans</item>
<item name="android:textStyle">bold</item>
</style>
To add the font, you'll need to do a few things:
res
folder and choose New > Android resource directory. Make sure you pick a resource type of "Font" (and probably the name as well.)font
folder under res
and choose New > Font resource file. Name the file product_sans.xml
.Make sure you add the app
namespace here if you're using the support library. Otherwise, if you're at SDK Version 26 or above, you can reference the android
namespace.
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:font="@font/product_sans_regular" app:fontWeight="400" app:fontStyle="normal" />
<font app:font="@font/product_sans_italic" app:fontWeight="400" app:fontStyle="italic" />
<font app:font="@font/product_sans_bold" app:fontWeight="700" app:fontStyle="normal" />
<font app:font="@font/product_sans_bold_italic" app:fontWeight="700" app:fontStyle="italic" />
</font-family>
More info about fonts in your XML can be found here: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
Here you can make a custom xml file in layout folder where you can add this:
<?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:textColor="#333333"
android:padding="10dp"
android:textStyle="bold" />
And then in your code mention it like this:
val adapter = ArrayAdapter.createFromResource(this, R.array.array_name, R.layout.custom_spinner) // where array_name consists of the items to show in Spinner
adapter.setDropDownViewResource(R.layout.custom_spinner) // where custom-spinner is mycustom xml file.
And then set the adapter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With