How do I change the text style of a spinner? - android

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.

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:
<?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, on your Activity Class when you are filling the Spinner with an array of items:
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'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:
<style name="SpinnerTextViewItem" parent="#android:style/Widget.TextView" >
<item name="android:textSize" >8dp</item>
<item name="android:textStyle" >bold</item>
</style>
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.

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
<?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>

When you create the Adapter that backs the Spinner you can set a layout for the spinner item.
spinner.setAdapter(new ArrayAdapter(this, R.id.some_text_view));
You can style some_text_view the way you want.
<TextView android:id="#+id/some_text_view" android:textStyle="bold" />

Actually you can customize spinner's text by xml.
In your own style, define a :
<item name="android:spinnerItemStyle">#style/yourStyleForSpinnerItem</item>
and define this style also :
<style name="yourStyleForSpinnerItem">
// Stuff you want for the item style.
</style>
When you instantiate the spinner's adapter in the java code, you can use the default android.R.layout.simple_spinner_item

If you don't want such workarounds, there's a simple way. Get the textview from the spinner 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

if you want just change background of popup View in spinner, call this method:
spinner.setPopupBackgroundResource(R.color.pa_md_white);

Related

Changing the font for the List Preference

i have a list preference in my application.
Have extended with a custom list preference.
Now i would like to keep the font of the preference entries according to my styling . (Don't want to have it changed according to the Device's fond settings).
I don't want to change any UI but only the styling like font size , font type etc.
Is it possible to do so. ?
cheers,
Saurav
You can create a custom style in XML, in a <resources> tag (not the listView layout), and then in the single item XML layout you can add Style = "#Style/mystyle" etc.
For example, there is a style.xml file already in values, and you could add:
<style name="ListViewStyle" >
<item name="android:textColor">#000000</item>
<item name="android:textSize">20dp</item>
</style>
and then in the listView xml:
<TextView
style="#style/ListViewStyle"
android:id="#+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

Change listpreference Summary text color and size

I have listpreference in my app , i want to customize it , i can set theme to prefs activity as bellow :
<activity
android:name=".Prefs"
android:label="#string/app_name"
android:theme="#style/PreferencesTheme">
preference style:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="PreferencesTheme" parent="android:Theme">
<item name="android:background">#FFDAB9</item>
<item name="android:textColor">#color/red_color</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
The above style changes the ListPreference background and ListPreference title text size and text color BUT it doesn't change ListPreference summary text color or size any way to do that ,
i tried this code but it doesn't work:
<item name="android:textColorSecondary">#color/red_color</item>
any help will be appreciated, thanks
Maybe it's too late to answer, but try to remove your line of color:
<item name="android:textColor">#color/red_color</item>
And add this which are for the primary, secondary and tertiary colors
<item name="android:textColorPrimary">#color/red_color</item>
<item name="android:textColorSecondary">#color/red_color</item>
<item name="android:textColorTertiary">#color/red_color</item>
You can choose the color you want to all those, honestly, I never saw the tertiary color in any preference page, but the primary is for the main and big text, the secondary is for the small text appears below the primary.
Hope it helps.
That sucks because the xml file says it uses textColorSecondary for the summary color.
What I would recommend you do is copy and save the contents of the file above into your own project, edit that file to look the way you want it to (Remember not to change any ids), and display a custom list preference item layout.
You can apply a custom preference layout in xml like so:
<ListPreference
android:key="pref_key"
android:title="#string/my_list_preference"
android:layout="#layout/custom_list_preference" />
I put this line into styles.xml and it works:
<item name="android:textColorSecondary">#00CC00</item>

Remove pressed color on Spinner

I'd like to remove the blue color (I'm testing my app with Holo) appearing when I click on a Spinner.
My code :
ArrayAdapter<String> array_adapter = new ArrayAdapter<String> (getActivity(),
R.layout.spinner_item, string_array);
array_adapter.setDropDownViewResource(R.layout.spinner_item);
Spinner spinner = (Spinner) getView().findViewById(R.id.spinner);
spinner.setAdapter(array_adapter);
spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:background="#drawable/item"
style="#style/EquidiaTheme.MySpinner" />
and item.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/transparent" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:drawable="#android:color/transparent" />
</selector>
This doesn't work. Any idea?
Customize android:spinnerDropDownItemStyle
Styling ActionBar dropdown menu
I was able to get rid the blue rectangle on item when I select it when I done this:
First I declare color that I want to use in values.xml:
<resources>
<drawable name="red_color">#ff0000</drawable>
<drawable name="blue_color">#0000ff</drawable>
<drawable name="green_color">#00ff00</drawable>
<drawable name="transparent_color">#00000000</drawable>
</resources>
Than I define custom style in Styles.xml
<resources>
<style name="Theme.Spinner" parent="android:Theme.Holo">
<item name="android:attr/listChoiceBackgroundIndicator">#drawable/transparent_color</item>
</style>
</resources>
in the style I was able to use only colors defined in resources ( Setting color like: #android:color/XXX or #XXX directly in style didn't work)
After all I aplly the them on Activity. I use Xamarin so may code is:
[Activity( Label = "TestLayouts", MainLauncher = true, Icon = "#drawable/icon", Theme="#style/Theme.Spinner")]
but for android it should be:
<activity android:theme="#style/Theme.Spinner">
I use this answer for reference:
Default selector background in Clickable Views
also to get rid the blue rectangle on spinner itself use:
<Spinner
android:background="#null"
You need to use both to completly remove blue rectable.
Also maybe this attributes in style may help you:
<item name="android:attr/colorPressedHighlight">#FF0000</item>
<item name="android:attr/colorLongPressedHighlight">#FF0000</item>
<item name="android:attr/listChoiceIndicatorSingle">#drawable/red_color</item>
A solution without theme. Ideal if you only got a few spinner.
create a drawable (xml) with state (https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList)
use the SAME image for the state PRESSED and NORMAL
then use it as background :
mySpinner.setBackgroundResource(R.drawable.my_spinner_state_drawable)
extra tips:
you can use system drawable (image) from the system resource: like "#android: drawable/btn_dropdown_normal". It is easier to maintain and give a more native look and feel.
ref http://androiddrawables.com/Buttons.html

Change font/typeface when button is pressed

I already how to change the style of an element with selector but I found nothing about the typeface...
Is it possible to do with ? Or is there another way?
Sadly, right now it is not possible.
There is a ticket for that in the Android code repo :
https://code.google.com/p/android/issues/detail?id=8941
Your best option is to manage it yourself in ontouch listeners (and yes this is ugly) or implement these new selectors yourself.
Typeface can easily be an element of your style... If you're using default android styles, then the idea would be to extend whatever style you're implementing and just change the elements you need. like the following style element, taken from the android styles and themes documentation:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
Then apply this style in your selector, just like you already know how to do.
The other option is, of course, to do it in code, but the selector is much cleaner
When button is clicked,Use below code to change font of the text(custom typeface).
Put your font under main->assets->fonts directory.
//change font when button pressed
val typeFace = Typeface.createFromAsset(activity!!.assets,"fonts/sf_semi_bold.ttf")
buttonAbout.setTypeface(typeFace)
TextView.setTypeFace() Use Ctrl+Space to show a giant list of classes for TextView or anything in general on.

set the textsize of a listview

I have a custom listview and have defined a style for it. I am not able to change the textsize of the items on listview. I am trying to change it through layout so that it can dynamically change depending on device.
styles.xml
<style parent="#android:attr/listViewStyle" name="ListStyle">
<item name="android:background">#android:color/white</item>
<item name="android:divider">#drawable/divider</item>
<item name="android:textSize">26dp</item>
<item name="android:textStyle">bold</item>
</style>
xml layout
<TvList style="ListStyle"
android:id="#+id/mainview"
android:layout_height="300dp"
android:layout_width="wrap_content" />
I know that i can use setTextSize in my code for changing the font size . But i want to change it applying styles.
Any suggestions on how to go about changing it?
Thanks
The size of the text within the List View is defined by the ListView's adapter's item resource. If you create a custom view for the list view item and define a TextView, you can change the size of the font within that text view, which will define the size of the text in each item within the List.
I would say the best way is to define resource files that you want and then override them in different configurations. For example, create a values/dimen.xml file and define:
<dimen name="text_size_medium">18sp</dimen>
and then create another file values-large/dimen.xml which defines
<dimen name="text_size_medium">22sp</dimen>
Then you just say in your styles.xml:
<item name="android:textSize">#dimen/text_size_medium</item>

Categories

Resources