I am using selector to change color of button. And I was trying to do same with text property.
Is it possible to change text in such way?
As far as I know, you can't do that because android:text="" takes string or string resource id as param. If you for example do something like this :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:text="SECOND" />
<item android:state_focused="true" android:state_pressed="true" android:text="SECOND" />
<item android:state_focused="false" android:state_pressed="true" android:text="SECOND" />
<item android:text="FIRST" />
</selector>
, like the way we are changing text color or background, it will put the path to this selector as your button text. It will be something like : res/drawable/text_selector.xml or whenever you put this file.
So the only way to achieve this is to control it manually in button's OnClickListener.
Related
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
I need to customize my android spinner. For this I created this spinner_selector.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/spinner_default" />
<item
android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/spinner_default" />
<item
android:state_pressed="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/spinner_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/spinner_default" />
<item
android:state_focused="true"
android:drawable="#drawable/spinner_focused" />
<item
android:drawable="#drawable/spinner_default" />
</selector>
and then set the background of spinner : android:background="#drawable/spinner_selector"
The problem is that the spinner looks really horrible. Before :
After :
and when it is pressed, it looks like this :
The image that I use is this :
How to solve this? I have this problem for Spinner, Button and EditText too. I really need to customize these components. Please help. Any idea is welcome.
To align text to right a bit, you can use padding in your XML please give Padding to the Spinner like this for ex: android:padding=5dp
The problem seems is the height and width of the Spinner, your need to change it,
either you can give any static value or use wrap content with setting image size bigger.
I have made my button transparent so I would like to have the button text color change when the button is pressed. Is it possible to do this using just xml files?
Yes, you can do it like that:
layout/main_layout.xml:
.....
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bonjour !"
android:textColor="#color/button_text_color"
/>
.....
color/button_text_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#c0c0c0" android:state_pressed="true"/>
<item android:color="#ffffff"/>
</selector>
See the section called State List in this bit of documentation...Drawable Resources.
You can define two different Button xml files one for the transparent 'default' state and another with the button as Red for your 'pressed' state. You then define a selector which switches the drawable resources in the different states.
EDIT: As per devunwired's comment the Color State List resource is probably more suitable for just changing colours rather than the drawable itself.
I like the solution proposed by Konstantin Burov in the other issue: Android customized button; changing text color
You can actually manage more states than just pressed and normal. But it should solve the problem.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#000000" />
<!-- Default color -->
<item android:color="#ffffff" />
</selector>
Then you can use that selector drawable in your button changing the text color attribute like below. Note that the selector in the example below is named "button_text_color"
android:textColor="#drawable/button_text_color"
Using the same drawable approach you can also solve the background color of the button. Just remember that in the selector instead of using the "android:color" attribute you need to use the "android:drawable" attribute like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#000000" />
<!-- Default color -->
<item android:drawable="#ffffff" />
</selector>
And then in the button itself do, note that this time the selector name is "button_background"
android:background="#drawable/button_background"
You have to do it in your code. Try this:
mBtn = ((Button) findViewById( R.id.button1 ));
mBtn.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
mBtn.setTextColor( Color.RED );
}
});
Declare:
private Button mBtn;
You must set #drawable xml resource in textColor attributte
Here is example: Android customized button; changing text color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:color="#FFFFFF" />
<item
android:state_pressed="true"
android:color="#000000" />
</selector>
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"/>
is it possible to create a skewed shape button?
(i.e like a stripe shape from bottom left of screen to top right of screen instead of the regular square or rectangle shape button)?
Something like this image http://www.codeproject.com/KB/silverlight/SilverLightFAQPart2/9.JPG
And I need to make sure the clickable area is also just the colored one.
Thanks,
Tee
You'd need to write your own class for that. Just subclass View and listen for the onClick events.
Try using a selector as background.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:background="#drawable/my_button"
/>
And then make a my_button.xml in your drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/focused" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/focusedpressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/pressed" />
<item android:drawable="#drawable/defaultbutton" />
</selector>
Source: http://www.anddev.org/tinytutcustom_button_backgrounds-better_imagebutton-t4298.html