What style attribute has to be changed for spinner dropdownitem? - android

I would like to change the color when a item of the spinner drop down is clicked.
I searched all the way through themes.xml, styles.xml and attr.xml but yet I can find which attribute has to be set to achieve this.
In attr.xml there is
<attr name="spinnerDropDownItemStyle" format="reference" />
In themes.xml in default themse there is
<style name="Theme">
...
<item name="spinnerDropDownItemStyle">#android:style/Widget.DropDownItem.Spinner</item>
<item name="spinnerItemStyle">#android:style/Widget.TextView.SpinnerItem</item>
...
When I follow this in style.xml I cant find any entry where the background drawable (with selectors for pressed and focused) is assigned (I guess there must be somewhere a refference to a xml file with selectors inside, like for button).
In short: I cant find what has to be set how in order to change the click and focus appearance of spinner dropdown items.
The Background alone can be changed by
<style name="CustomSpinnerAppearance0" parent="android:style/Widget.Holo.Light.Spinner">
<item name="android:background">#drawable/spinner_default0</item>
<item name="android:dropDownSelector">#ffc0c0c0</item>
<item name="android:popupBackground">#ffc0c0c0</item>
</style>
But even when I set a selector or transition as popupBackground nothing changes in the click behaviour.
Any ideas?
Thanks.

Meanwhile I found the solution.
To close this question:
I still dont know, why it has no influence on the behaviour when I set the dropDownSelector.
But changing the attribute
<item name="android:listChoiceBackgroundIndicator"></item>
does the trick.

Related

How to change Background color of menuItem in android?

I am working on programmatically add a menu-Item into menu. I want to add background color on selection to particular Item. How can I add background to menuItem?
Your answer would be appreciated.
While other answers provide change style (which will affect all menu items, and, as I understand, need is to change one menu item),I propose you to use android:actionLayout attribute to implement your custom layour for menu item.
See documentation for it (search actionLayout there, it's very simple actually).
Also I think there is a possibility to make a selector as your layout and use it.
Update. Sorry, I mislead you :(
All of this will work only if MenuItem is shown as Action (not when pressing on three dots).
Seems like guys given other answers were right - the only way to customize this is changing themes.
But please check this and this - it might help you to provide selector for your purpose.
Also you can create PopupMenu or even PopupWindow, last one is fully customizable.
The last option is to create custom Spinner, check this.
Hope something from this will help you. Thanks.
Firstly You need to create an Style for popmenu as you want refer below sample for that
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:textStyle">#style/commonEditTextTheme</item>
<item name="android:popupBackground">#drawable/pop_up_menu_bg_with_shadow</item>
</style>
Place of drawable you can also replace with color as you needed or make XML Drawable into your drawable folder
<style name="commonEditTextTheme" parent="#android:style/TextAppearance.Medium">
<item name="android:fontFamily">sans-serif-light</item>
</style>
this pop menu theme add in your main application or activity theme like below
<!--My Theme-->
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
......
<item name="android:popupMenuStyle">#style/MyApp.PopupMenu</item>
</style>

AutoCompleteTextView dropDownSelector

I have an AutoCompleteTextView component and I want to change its default drop down selector to another color. First I tried: android:dropDownSelector="#FF400000", but it caused no selector to appear at all! Next I put the color in a drawable resource:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/holo_blue_dark"/>
</shape>
And linked it: android:dropDownSelector="#drawable/drop_down_selector", but that did the same effect (no selector). Next I found a similar issue someone opened, just for spinner: http://code.google.com/p/android/issues/detail?id=24922, so I tried defining a style as was explained in the solution there:
<style name="AutoCompleteDropDown" parent="#android:style/Theme.Holo.Light">
<item name="android:dropDownListViewStyle">#style/AutoCompleteSelector</item>
</style>
<style name="AutoCompleteSelector" parent="#android:style/Widget.Holo.Light.ListView">
<item name="android:listSelector">#FF400000</item>
</style>
And linked it: style="#style/AutoCompleteDropDown", but it did nothing (however, the default selector got back since I removed the dropDownSelector attribute).
So what am I missing here? What am I doing wrong?
UPDATE: Ok, so as was suggested, I also tried a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="true"
android:state_focused="true" android:state_activated="true"
android:drawable="#android:color/holo_green_light"/>
</selector>
But it didn't work, I still get no selector (at least not one I can see).
Theme's and styles are different by intent. And use different attributes. Theme define style for different widgets. Style define the widgets itself. Theme's are applied to application or activities.
1) If you want it to be for just one widget.
You need to go to /platforms//res/values/values.xml, find out how styles is defined for your widget (AutoCompleteTextView). Pick up required attribute. Define the same selector like in system but with your modifications. And you can even find out the selector in /res/drawable
2) If you want to be it all over the application:
a)You need to go to /platforms//res/values/themes.xml
b) There you can find out which style are in the theme you chose for AutoCompleteTextViewb
<item name="autoCompleteTextViewStyle">
#android:style/Widget.AutoCompleteTextView
</item>
c) Then got to res/values/styles.xml
d) There you can find out the style for widget.
e) Then you need to extend theme. And override autoCompleteTextViewStyle attribute by your new created style like in 1 option.
Try remove background from your views, which generated by adapter. That has helped me.

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.

Android color selector doesn't work with custom attributes

I've got in attrs.xml
<resources>
<!-- theme specific colors -->
<attr format="reference|color" name="foreground" />
<attr format="reference|color" name="background" />
</resources>
And then in theme.xml
<style name="MyTheme" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="foreground">#0000FF</item>
<item name="background">#00FF00</item>
</style>
I also created color selector named forground_to_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="?background"/> <!-- pressed -->
<item android:state_focused="true" android:color="?background"/> <!-- focused -->
<item android:color="?foreground"/> <!-- default -->
</selector>
Now I'd like to use it all together in TextView:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/forground_to_background" />
Unfortunately it doesn't work. Instead of having nice green blue colors I've got only one color - red. TextView is always red. When I change TextView to use "?foreground" color will change. Also when I change in colors selector from "?xxxx" to hardcoded value as "#00f" color start to work.
Where is problem? What am I doing wrong?
Edit:
I believe it is duplicate of problem/bug Can a selector resource use a color defined in a style?
Edit2:
Moreover when I try use this TextView in ListView application crashes. It cannot parse XML.
You cannot reference ?attr/ when choosing colors for a selector. What you can do, if you want per-theme colors in your selector, is create multiple selectors which reference #color/ and #drawable/, and then have a "reference" attr which associates one of the selectors with the given style.
<attr name="forground_to_background" format="reference" />
You then have to set the text color like
android:textColor="?attr/forground_to_background"
I believe the text was always red because Android was interpreting the attr's integer value as a color (red), rather than using it as a lookup for what you actually wanted.
The reason why this happens is that I have different Context. While inflating Context is aware of my theme attrs, but to the ListView adapter I passed ApplicationContext that wasn't aware of those attrs. Now I don't know why it doesn't know about them ;)
Are you sure if you applying MyTheme to the activity or the textview?
Another thing you can try is that instead of using the "?" operator in your forground_to_background.xml, trying using "#" instead. see if that fixes your problem

Different formats for different Textview defined by Custom style

I have a problem figuring out how to do this:
I am currently coding an app that comes with different themes (User can select the complete appereance of the app out of a list of different styles).
Then the list item is selected I want to call setTheme(R.style.Own_App_Style0); to change the complete appearance.
The problem is best explained by an example:
Lets say we have 2 TextView.
Theme1
1. TextView: TextColor should be green and TextSize 15sp.
2. TextView: TextColor should be red and TextSize 10sp.
Theme2
1. TextView: TextColor should be blue and TextSize 10sp.
2. TextView: TextColor should be yellow and TextSize 10sp.
Of course I know that by setting <item name="textViewStyle">#android:style/Widget.TextView</item> I can change the default appearance of TextViews.
But how can it be done to have lets say two (ore more) different types of TextView with different applied styles (and by xml)?
Found a solution (basically in this answer setTextAppearance through code referencing custom attribute). In case anyone else has this problem I shortly explain:
Declare in style.xml a attribute and in the actual style definition asign a value (reference) to this attribute:
<declare-styleable name="CustomTextView">
<attr name="mainTextView" format="reference"/>
</declare-styleable>
<style name="appstyle0" parent="android:style/Theme.Holo.Light">
<item name="#attr/mainTextView">#style/CustomTextViewAppearance1</item>
<item name="android:textViewStyle">#style/CustomTextViewAppearance2</item>
</style>
<style name="appstyle1" parent="android:style/Theme.Holo.Light">
<item name="#attr/mainTextView">#style/CustomTextViewAppearance2</item>
<item name="android:textViewStyle">#style/CustomTextViewAppearance1</item>
</style>
<style name="CustomTextViewAppearance1">
<item name="android:textSize">10dip</item>
</style>
<style name="CustomTextViewAppearance2">
<item name="android:textSize">30dip</item>
</style>
Now in the layout all textViews are like CustomTextViewAppearance2 (because this is set as standard in this style. And the textViews that should use the other style write into the definition:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blablabla"
style="?mainButtonTextView"/>
When you now call .setTheme (after restart the activity) the appearance of the textviews switch. Like this method you can define as many different types of View styles and switch between them only by calling .setTheme.
Unfortunately, styles are static once they are defined. To have an entire cascade of styles modified programmatically, you would have to change the definition of the style itself. Instead, all you can do is change the style that is assigned to a TextView (or whatever style-able object), as outlined in the question I linked to in my comment above.

Categories

Resources