Creating TextViews with RadioButton/RadioGroup Behavior (Or Vice Versa) - android

I wanted a clickable TextView that changes color and holds it, that changes previously changed text back to default color. And of course I wanted some state to be changed when each TextView was "checked." Basically a TextView that acts like a RadioButton, or a RadioButton minus the button.
At first I tried to come at from the TextView angle. But it seems like using RadioButtons and adding text behavior is easier since RadioButtons extend TextView anyway.
So I have this color resource applied to each RadioButton:
<?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:color="#0000ff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#0000ff" />
<item android:state_focused="false" android:state_pressed="true" android:color="#0000ff" />
<item android:state_checked="true" android:color="#0000ff" />
<item android:color="#ff00ff00" />
</selector>
That gets me the text behavior I want. Now I just need a way to hide the RadioButton itself. Ideally this would be in XML so I can just apply a style to various RadioButton, but I'm not going to turn my nose up at a RadioButton extension.
So, anybody know how to hide the RadioButton button itself?

You should use CheckedTextView. It supports checked state.
OR
You can use RadioButton and set its button to null:
<RadioButton
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/selector"
android:button="#null"
android:paddingLeft="0dip"/>

Related

Button change color

I have two buttons in layout,let suppose button A and button B and I want that when user touch any of two button,their background color should change for that moment.
code
``
<item android:state_hovered="true"
android:drawable="#drawable/state_hovered"/>
<item android:state_pressed="true"
android:drawable="#drawable/state_pressed"/>
<item android:drawable="#drawable/state_deafult" />
``
State_pressed working...but state_hovered is not working.
So,please suggest a way to do so.
Thanks in advance.
Change color while button is pressed?
See an answer to this question, asked previosly in StackOverflow:
Create a my_button_background.xml file in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/blue" />
<item android:state_focused="true" android:drawable="#color/gold" />
<item android:drawable="#color/grey" />
</selector>
And use this in layout file:
android:background="#drawable/my_button_background"
And read more about Android's Color State. Basically, you can assign colors, drawables, shapes, etc. to different states of views, such as enabled, disabled, focused, clicked, etc.

Android RadioButton textColor selector

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

Change button text using <selector>

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.

Customize the look of checkbox starStyle

I the following inside the xml layout for my ListView row:
<CheckBox
android:id="#+id/favbutton"
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
And it shows a blue star which can be checked to select a favourite. This is exactly the functionality that I need, but I just would like to customize the star icon that is shown. I would like to change the colour to yellow instead of blue and also make the star smaller if possible. Is it possible to make these changes by making changes to the theme applied to the application? What would I need to edit to change the look of the star icon?
I resolved this as follows. I created a file in res/drawable called btn_star_selector.xml and defined the selector as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="#drawable/btn_star_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/btn_star_off_pressed" />
<item android:state_checked="true" android:drawable="#drawable/btn_star_on" />
<item android:state_checked="false" android:drawable="#drawable/btn_star_off" />
</selector>
With this selector defined I then replaced this line in the checkbox definition
style="?android:attr/starStyle"
with this
android:button="#drawable/btn_star_selector"
I then created four images to represent the different states of the checkbox, namely on, off, on pressed and off pressed and also placed these in res/drawables.
Hope this helps anyone else that is trying to figure out how to customize checkboxes.

using standard Android button but with modifications

I'm trying to use the the standard android button in my layout, but make the background transparent when not pressed, or making text left or right justified, or whatever custom modification I want. Is there some simple way I'm missing to inherit from the standard button but change a few properties? I've looked at the two posts below, and can't get them to work and am too new to leave comments on those pages, plus both of those solutions have problems anyway:
I've tried copying #android:drawable/btn_default source per How to disable the default Button color changing property on onClick, but all of the resources linked to from there are private. I tried to find the source for those private files, but some of them i can't find even if i go into the android sdk folder to get the raw files. Where can i find these files, if this is the way to edit the standard button? copying those private files is definately not ideal though, if the standard selected/pressed/whatever button changes in another api i'll still be using the old ones in this case and have inconsistent buttons...
Also, I've seen Standard Android Button with a different color which is good for making custom buttons in general, but how do I set it to be exactly like the standard button? i.e. what are the colors, are the gradients right, etc. Again, this has the problem that if standard button changes i'll still be using old values.
Use styles on your buttons:
<Button id= ... style="#style/myButton" />
values/styles.xml:
<style name="myButton" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/myBackground</item>
</style>
To deal with various button states (e.g. pressed, etc) you'll need a selector drawable resource, example drawable/myBackground.xml:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states
-->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/button_unfocused" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/button_unfocused" />
<!-- Focused states
-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/button_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/button_focus" />
<!-- Pressed
-->
<item android:state_pressed="true" android:drawable="#drawable/button_press" />
</selector>
The drawable/button_press.xml could specify a gradient, shape, borders, etc, as you need.
An example button_press.xml that does a background gradient, rounded corners, and border:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF404040" />
<corners android:radius="6dp" />
<gradient android:startColor="#FF9B00" android:centerColor="#FFB300" android:endColor="#FFCA00" android:angle="90" />
</shape>
The first part of CSmith's answer about using styles with parent="#android:style/Widget.Button" is the best way to change button properties like left or right justified text. However the rest of the answer describes how to create your own selectors, so I thought I'd add my solution to inherit selectors from the default button, and then just override specific ones you want to change:
just put android:drawable="#android:drawable/btn_default" for those selectors you want default behavior for, and then you can just specify custom fields for only those selectors you want to change. in my case of trying to have a button that inherits all the selectors but has a transparent background normally instead of the gray one, do the following (duplicating the first part of CSmith's answer here for clarity, thanks CSmith!):
Define a button using a custom style:
<Button id= ... style="#style/myButton" />
res/values/styles.xml:
<style name="myButton" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/myBackground</item>
...other changes to default button...
</style>
then, to inherit all selectors and just overwrite the ones that show gray:
in res/drawable/myBackground.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="#drawable/transparent_btn" />
<item android:state_pressed="true"
android:drawable="#android:drawable/btn_default" />
<item android:state_focused="true"
android:drawable="#android:drawable/btn_default" />
<item android:drawable="#drawable/transparent_btn" />
</selector>
and, in res/drawable/transparent_btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#android:color/transparent" />
</shape>
NOTE: this example doesnt worry about state_enabled, if you want specific behavior for whether a button is enabled or not just overwrite those selectors as well.

Categories

Resources