So I have this buttons:
What I want is to stop users from checking or unchecking any of them. Anyway i tried with android:enable = "false" but it does not seems to be a solution since the buttons gets faded and that is something i do not want
Set your both radio button clickable="False"
By xml
android:clickable="false"
by Programmatically
radiobuttonOne.setClickable(false);
radiobuttonTwo.setClickable(false);
it works for you
You can set your drawables on RadioButton:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/btn_radio"
android:text="RadioButton" />
btn_radio.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/btn_radio_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/btn_radio_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/btn_radio_on_selected" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/btn_radio_off_selected" />
<item android:state_checked="false" android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_radio_on" />
</selector>
Replace the btn_radio_on and btn_radio_off_selected with your own drawables.
Do as you did before with android:enable = "false" but only change the disabled/enabled colour of your radio button in your styles.xml, add these lines to your App Theme:
<item name="colorControlActivated">your_color_for_activated</item>
<item name="colorControlNormal">your_color_for_normal</item>
RadioButton extends CompoundButton
android:clickable="false"
android:enabled="false"
android:checked="true"
android:focusable="false"
android:clickable = "false" seemed to be the answer of the question but the problem was that i was setting onClickListener on the buttons which actually calls setClickable(true). I just made it programmatically after the onClickListener. Now it works
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
It looks like we can use the following in a RadioButton:
android:button="#drawable/myCustomStateBackground"
but that drawable only occupies the spot where the radio drawable would normally go. Ideally I want my entire button background to be stateful. So when pushed, I want the entire button to look like its stuck in the pressed state. To do that, I was hoping I could do something like:
android:button="null"
android:background="#drawable/myCustomStateBackground"
but then the background drawable doesn't know about push state, like the button attribute does. Is there a way around it?
Give your radiobutton a custom style:
<style name="MyRadioButtonStyle" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/custom_btn_radio</item>
</style>
custom_btn_radio.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/btn_radio_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/btn_radio_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/btn_radio_on_selected" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/btn_radio_off_selected" />
<item android:state_checked="false" android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_radio_on" />
</selector>
Replace the drawables with your own.
You should set android:button="#null" instead of "null".
You were soo close!
full solution here:
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/oragne_toggle_btn"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/oragne_toggle_btn"
android:layout_marginTop="20dp"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/oragne_toggle_btn"
android:layout_marginTop="20dp"
android:text="RadioButton" />
</RadioGroup>
selector XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/orange_btn_selected" android:state_checked="true"/>
<item android:drawable="#drawable/orange_btn_unselected" android:state_checked="false"/>
</selector>
if you want to change the only icon of radio button then you can only add android:button="#drawable/ic_launcher" to your radio button and for making sensitive on click then you have to use the selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_what_you_want_on_select_state" android:state_checked="true"/>
<item android:drawable="#drawable/image_what_you_want_on_un_select_state" android:state_checked="false"/>
</selector>
and set to your radio android:background="#drawable/name_of_selector"
In programmatically, add the background image
minSdkVersion 16
RadioGroup rg = new RadioGroup(this);
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(R.drawable.account_background);
rg.addView(radioButton);
I have a problem with a TableRow, which I add dynamically.
private void addRow(String body) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow row = (TableRow) inflater.inflate(R.layout.customrow,null);
TextView name = (TextView) row.findViewById(R.id.customName);
name.setText(body);
row.setOnLongClickListener(this);
}
I would like this row to change color upon onClick and onLongClick.
Code in the customrow.xml file is:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableRow1"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_vertical"
android:onClick="showOnClick">
<TextView android:id="#+id/customName"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="5">
</TextView>
</TableRow>
I have tried to use android:background="#drawable/clickedbackground"with the row but it is not working.
Code in the clickedbackground.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:drawable="#color/custom" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#color/custom" />
</selector>
Anyone knows what I am doing wrong (color/custom is defined in another xml which works)?
Thank you
You are creating object for tablerow named row. and you have also clickedbackground.xml file. just use below code in addRow method.
row.setBackgroundResource(R.drawable.clickedbackground);
I think it solves your problem.
In your addRow() method you're inflating the row but you're not adding it to any parent layout, and as row is a local variable I think you're not doing it anywhere else, is it a copy/paste problem?
Again, your customrow.xml might be not working because the opening TableRow tag lacks the closing >, but it might be copy/paste problem.
Using android:background="#drawable/bg" with bg being a selector is a common pattern and it should work. You might want to simplify your selector: you don't need to specify all the states for each item and all the combinations. It works with a "first match", so this will do the job:
<?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/custom" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_focused="true" android:drawable="#color/custom" />
<item android:drawable="#android:color/transparent" />
</selector>
Also, notice that selected and focused are two different states, focused being the one you get when moving around with dpad.
If this didn't help please specify what "is not working" means: what do you expect? what's happening instead?
Adding
<resources>
<style name="row" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/rows</item>
</style>
</resources>
in styles.xml and setting
style="#style/row"
in the TableRow did the job.
where the rows.xml in the drawable is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/blue" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/custom" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#color/white" />
<item
android:state_enabled="true"
android:drawable="#android:color/transparent" />
</selector>
do not forget to add to the style
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
Otherwise, you will not be able to use the states of a rowLayout.
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);