How to change the text style for the counter?

I create a spinner in my layout XML files and set a string array for this counter. Changes to the text do not change.

I read on googlegroups that the spinner has no text, and therefore the text style cannot be changed, and I need to change the style of the text presentation shown in spinner. But how can I do this. Preferably in my xml file.

+7
android layout spinner
source share
6 answers

When creating an adapter that supports Spinner, you can set the layout for the spinner element.

spinner.setAdapter(new ArrayAdapter(this, R.id.some_text_view)); 

You can style some_text_view however you want.

 <TextView android:id="@+id/some_text_view" android:textStyle="bold" /> 
+13
source share

As my predecessor pointed out, you cannot do this in the main XML layout file where the Spinner component is located.

And the answer above is good, but if we want to use the best practices of Google, as you know ... use styles for everything ... you could do it with 3 simple steps as follows:

Step 1: You need an additional file in your layout folder with a search for Spinner elements:

 <?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/textViewSpinnerItem" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/SpinnerTextViewItem" xmlns:android="http://schemas.android.com/apk/res/android" /> 

Name this file: spinner_item_text.xml

Step 2: Then in the activity class, when you populate Spinner with an array of elements:

 adapter = new ArrayAdapter<CharSequence>(this, R.layout.spinner_item_text, items); spinner.setAdapter(adapter); 

Note that the R.layout.spinner_item_text resource is in your own R.

Step 3: In the folder with your values, create or use (you already have) the styles.xml file. The required style entry should look like this:

 <style name="SpinnerTextViewItem" parent="@android:style/Widget.TextView" > <item name="android:textSize" >8dp</item> <item name="android:textStyle" >bold</item> </style> 

What is it!

Until now, it was really very convenient to place all the text sizes, styles, colors, etc. in styles.xml, so it’s easy to maintain.

+19
source share

XML only

As a follow-up to @Cyril REAL's excellent answer, we'll take a closer look at how to style your Spinners only through XML, if you populate Spinner through android:entries .

The above answers work if you create your Spinner via code, but if you customize Spinner entries via XML, i.e. using android:entries , you can adjust the text size and other attributes using the following two theme options

In your res / values ​​/styles.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppBaseTheme" parent="android:Theme.Holo"> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- For the resting Spinner style --> <item name="android:spinnerItemStyle"> @style/spinnerItemStyle </item> <!-- For each individual Spinner list item once clicked on --> <item name="android:spinnerDropDownItemStyle"> @style/spinnerDropDownItemStyle </item> </style> <style name="spinnerItemStyle"> <item name="android:padding">10dp</item> <item name="android:textSize">20sp</item> <item name="android:textColor">#FFFFFF</item> </style> <style name="spinnerDropDownItemStyle"> <item name="android:padding">20dp</item> <item name="android:textSize">30sp</item> <item name="android:textColor">#FFFFFF</item> </style> </resources> 
+12
source share

In fact, you can customize the spinner text using xml.

In your own style, define:

 <item name="android:spinnerItemStyle">@style/yourStyleForSpinnerItem</item> 

and also define this style:

 <style name="yourStyleForSpinnerItem"> //stuff you want for the item style </style> 

When you instantiate the spinner adapter in java code, you can use the default android.R.layout.simple_spinner_item

+9
source share

If you do not need such workarounds, then there is an easy way. Get a text image from the counter and change its parameters:

 TextView tv = (TextView) spin.getSelectedView(); tv.setTypeface(Typeface.DEFAULT_BOLD); //to make text bold tv.setTypeface(Typeface.DEFAULT); //to make text normal 
+2
source share

If you just want to change the background of the View in spinner popup, call this method:

 spinner.setPopupBackgroundResource(R.color.pa_md_white); 
0
source share

All Articles