I have a selector for textColor of a RadioButton like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#fff"/>
<item android:state_focused="true" android:color="#f00"/>
<item android:state_pressed="true" android:color="#0f0"/>
<item android:state_focused="false" android:state_pressed="false" android:color="#00f"/>
</selector>
I expected that the one selected RadioButton will have different color than the others.
However, all of the RadioButtons have blue text (using android:state_focused="false" android:state_pressed="false"), even the one that is selected.
What am I doing wrong?
It looks like you're just using the wrong selectors. The docs describe selecting as follows:
During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.
Source link
So, in order:
state_selected is never true as RadioButtons use state_checked
when checked.
state_focused is never called because RadioButton
will never receive input focus.
state_pressed should be working.
When you hold your finger down you don't see the text appearing
green?
state_focused false and state_pressed false ends up being
default so you see blue.
If you would like to see different states, try these:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#0f0"/>
<item android:state_checked="true" android:color="#fff"/>
<item android:color="#00f"/>
</selector>
I have tested the above and can see all colors being expressed appropriately.
According to Android.
https://developer.android.com/guide/topics/resources/color-list-resource.html.
https://developer.android.com/reference/android/content/res/ColorStateList.html
You have to create a folder called 'color' in 'res' directory and create a new file called radiobuttonstate.xml for example which it looks like this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:color="YOUR COLOR" />
<item
android:state_pressed="true"
android:state_enabled="false"
android:color="YOUR COLOR" />
<item android:color="YOUR COLOR"
android:state_checked="true"/>
<item
android:state_enabled="false"
android:color="YOUR COLOR" />
<item android:color="YOUR COLOR" />
</selector>
then in your radio button define in the android:textColor attribute your color list you previously defined.
<RadioButton
android:id="#+id/radio_H"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="#string/string_example"
android:textColor="#color/radiobuttonstate"
android:textAlignment="center" />
The answer provided by #GrantAmos is perfect and working. If you want to text color selector through XML, please use this code.
android:textColor="#color/textview_selector"
However, if you want to set the selector programmatically, use this code -
radioButton.setTextColor(ContextCompat.getColorStateList(getContext(), R.color.textview_selector));
Hope it will save someone's time.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#color/dark_grey"/>
<item android:state_checked="true" android:drawable="#color/topic_green"/>
</selector>
This one works for me. Actually when i use android:color="#color/dark_grey". It didn't work. But when i changed to drawable it did.
<style name="MyRadioButton" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/lbl_login</item>
<item name="colorControlActivated">#color/btn_login_back_color</item>
</style>
<RadioButton
android:id="#+id/btn_radio_credit_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_8sdp"
android:fontFamily="#font/poppins_regular"
android:paddingStart="#dimen/_14ssp"
android:paddingEnd="#dimen/_1sdp"
android:text="Credit Card"
android:textColor="#color/lbl_login"
android:textSize="#dimen/_14ssp"
android:theme="#style/MyRadioButton" />
Refrence
Related
I have a recycler view which I need to use one of the color selectors, depending on what value in the data binding, to change the TextView color.
I have two selectors:
color/selector_item_text.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_checked="true" />
<item android:color="#color/white" android:state_pressed="true" />
<item android:color="#color/white" android:state_activated="true" />
<item android:color="#color/black" />
</selector>
color/selector_item_textwithspecial.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/pink" android:state_checked="true" />
<item android:color="#color/pink" android:state_pressed="true" />
<item android:color="#color/pink" android:state_activated="true" />
<item android:color="#color/orange" />
</selector>
And I'm binding it to my TextView like this:
<TextView android:text="#{data.displayPrice}"
android:textColor="#{data.isSpecial ? #color/selector_item_textwithspecial : #color/selector_item_text}"
style="#style/ProductPrice"/>
The problem is that the TextView color is always orange (if it has special) or black. The selection never changes the color. However, if I remove the databinding, it worked as expected.
For example, the following will make TextView becomes pink (when selected) and orange (when not selected)
<TextView android:text="#{data.displayPrice}"
android:textColor="#color/selector_item_textwithspecial"
style="#style/ProductPrice"/>
Any idea how to tackle this problem?
Thanks...
Figured it out, see here
use android:textColor="#{data.isSpecial ? #colorStateList/selector_item_textwithspecial : #colorStateList/selector_item_text}"
I think you need to use ContextCompat to get the colors.
<TextView android:text="#{data.displayPrice}"
android:textColor="#{data.isSpecial ? #{ContextCompat.getColor(context, #color/selector_item_textwithspecial)} : ContextCompat.getColor(context, #color/selector_item_text)}"
style="#style/ProductPrice"/>
while context you need to declare in your root element in your xml file
tools:context="...."//path to your activity
I'm struggling with ListView selector. I want to have gray color on list item when the user has his finger on the screen. That's all. When the user releases the finger (or move it elsewhere) the color should back to white as it should be all the time. This is my ListView:
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:textAppearance="?android:textAppearanceMedium"
android:drawSelectorOnTop="false"
android:listSelector="#drawable/my_list_selector"
android:id="#+id/list"/>
This is selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"
android:state_activated="true"/>
<item android:drawable="#color/colorPrimaryDark"
android:state_activated="false"/>
</selector>
What am I doing wrong in here?
P.S. If true and false are given in the opposite way nothing happens.
Seems like you want to use android:state_pressed instead of android:state_activated
code
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"
android:state_pressed="true"/>
<item android:drawable="#color/colorPrimaryDark"/>
</selector>
After the discussion with OP and #pskink the issue was resolved with
<item android:state_window_focused="false" android:drawable="#color/transparent"/>
I've created custom ToggleButtons in Android and since all buttons inherit from the same xml I want to change how they act depending on state, so when the state is checked I want to change the shadow color but this does not seem to possible with the current SDK.
I've created an xml file which holds button_colors:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:color="#FFFFFF" />
<item
android:color="#000000" />
</selector>
But this only seems to work with text-color and not shadow color on the text.
Is there something I'm missing?
And rather not do this for every button manually in code since I want this to apply to every button in the app.
UPDATE EDIT:
My selector currently looks like this
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/button_gradient_selected" />
<item
android:drawable="#drawable/button_gradient" />
</selector>
But as I mentioned to the commentator below I can't seem to change the style/text-color-shadow from here since it only can take in a drawable it seems.
When I try to put in a different style on the button in here it force closes or either does not change the style depending on state. When I only try to put in the style here and have the drawable be set in the style it force closes. Either way it does not work it seems.
Seems that the Android framework does not support this.
From TextView.java:
case com.android.internal.R.styleable.TextView_textColor:
textColor = a.getColorStateList(attr);
break;
case com.android.internal.R.styleable.TextView_shadowColor:
shadowcolor = a.getInt(attr, 0);
break;
They treat textColor and shadowColor differently.
Please refer to my solution on a different StackOverFlow question. I extended TextView to give a working solution here. (Replace TextView with Button)
This is the Selector file you have to implement:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/btn_toggle_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_toggle_on" />
</selector>
These are the picture tey used for the default ToggleButton:
btn_toggle_on and btn_toogle_off
You can have a selector for the shadow color like this: color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_pressed="true"
android:color="#color/btn_text_on" />
<item
android:state_focused="true"
android:color="#color/btn_text_on" />
<item
android:color="#color/btn_text_off" />
</selector>
and then use this selector while styling your button in styles.xml like this:
<style name="ButtonStyle">
<item name="android:textColor">#FF383C48</item>
<item name="android:textSize">12sp</item>
<item name="android:shadowColor">#drawable/color_selector</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">1</item>
<item name="android:typeface">sans</item>
<item name="android:textStyle">bold</item>
</style>
I have an issue with my ImageButton not changing state. When I click, or rather touch, the button it stays as the same image. Here is the XML I am using as a selector.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/pushed" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:drawable="#drawable/default" />
</selector>
I call this selector from my main.xml as
android:background="#drawable/imagechoice"
imagechoice.xml is the file with the selector
I don't understand why this is not working, unless I have to have some java code, but everything I've seen said this should work.
When using an ImageButton, isn't it the 'src' property you should use and not background?
Make sure that you copy the same images and the button XML into every "drawable" folders (hdpi,ldpi,mdpi).
That's how I solved this problem on my app.
Good luck :)
I have nearly the same XML and it works just fine. Are you sure you're not replacing the drawable in code somewhere?
On another note, your XML can be simplified by using the cascading nature of the state matching.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/pushed"
/>
<item android:state_focused="true"
android:drawable="#drawable/pushed"
/>
<item android:drawable="#drawable/default"
/>
</selector>
This is my xml of a button with my own custom image on it and it works great:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/btn_off" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:drawable="#drawable/btn_off" />
</selector>
Make sure you set Image button background as mentioned below.I think you are not setting the selector as background instead you are setting the image as background.
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_up_selector"
android:text="1"
android:textColor="#fffafa"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"/>
I am wanting to allow the user of my android application the ability to set some parameters. The radio button is ideal for this situation. However, I don't like the radio buttons are rendered.
Is it possible to change the radio button icon? For example, is it possible to create a custom layout for each row and in that layout reference my own icon and change the font et al.
Yes that's possible you have to define your own style for radio buttons, at res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:radioButtonStyle">#style/RadioButton</item>
</style>
<style name="RadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/radio</item>
</style>
</resources>
'radio' here should be a stateful drawable, radio.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/radio_hover" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/radio_normal" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/radio_active" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/radio_active" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/radio_hover" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/radio_normal_off" />
<item android:state_checked="false" android:drawable="#drawable/radio_normal" />
<item android:state_checked="true" android:drawable="#drawable/radio_hover" />
</selector>
Then just apply the Custom theme either to whole app or to activities of your choice.
For more info about themes and styles look at http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/ that is good guide.
You can put custom image in radiobutton like normal button.
for that create one XML file in drawable folder
e.g
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/sub_screens_aus_hl"
android:state_pressed="true"/>
<item android:drawable="#drawable/sub_screens_aus"
android:state_checked="true"/>
<item android:drawable="#drawable/sub_screens_aus"
android:state_focused="true" />
<item android:drawable="#drawable/sub_screens_aus_dis" />
</selector>
Here you can use 3 different images for radiobutton
and use this file to RadioButton like:
android:button="#drawable/aus"
android:layout_height="120dp"
android:layout_width="wrap_content"
The easier way to only change the radio button is simply set selector for drawable right
<RadioButton
...
android:button="#null"
android:checked="false"
android:drawableRight="#drawable/radio_button_selector" />
And the selector is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_checkbox_checked" android:state_checked="true" />
<item android:drawable="#drawable/ic_checkbox_unchecked" android:state_checked="false" /></selector>
That's all
yes....`
from Xml
android:button="#drawable/yourdrawable"
and from Java
myRadioButton.setButtonDrawable(resourceId or Drawable);
`
Here's probably a quick approach,
With two icons shown above, you shall have a RadioGroup something like this
change the RadioGroup's orientation to horizontal
for each RadioButton's Properties, try giving the icon for Button
under CompoundButton,
adjust the Padding and size,
and set the Background attribute when checked.
In case you want to do it programmatically,
checkBoxOrRadioButton.setButtonDrawable(null);
checkBoxOrRadioButton.setBackgroundResource(R.drawable.resource_name);