Change RadioButton highlight colour when pressed/tapped - android

I have some RadioButtons inside a RadioGroup and I am using custon icons to replace the typical radio "dot".
I have my normal state (checked=false) and checked state (checked=true) sorted.
However, when tapping/pressing the RadioButton, there is a grey-ish highlight colour in the background, that is round in shape (to match the original radio dot).
How can I customise this highlight colour? I tried using "android:state_pressed" but that didn't seem to do anything? (Well, I tried adding a shape, as mentioned here: https://stackoverflow.com/a/14602078/601869)
Ideally, I'd like the shape to match that of the icon (well, most probably be a bit bigger) and change the colour.
Selector XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/rounded" />
<item android:state_checked="true" android:drawable="#drawable/rounded_checked" />
</selector>
RadioButton XML:
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioButton01"
android:button="#drawable/rounded_radio_select" />

try changing
android:button="#drawable/rounded_radio_select" />
to
android:background="#drawable/rounded_radio_select"
android:button="#android:color/transparent"/>
Maybe this is the same issue Adding custom radio buttons in android

Related

Remove border RadioButton?

Can I remove border of RadioButton and I have RadioButtons like bellow :
I see a lot of question and I used from android:buttonTint="" and style ... but don't work .
You can't remove that using the radiobutton.
what you can do is set it to the same color as your background,
or create a custom radiobutton XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false"
android:drawable="#drawable/your_radio_off_image_name" />
<item android:state_checked="true"
android:drawable="#drawable/your_radio_on_image_name" />
</selector>
use that XML in your layout:
<RadioButton
android:id="#+id/radiobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_radiobutton"/>
more info and examples about custom xml:
Adding custom radio buttons in android
remove default radio button checkbox when using a custom selector
Another option is to use a toggle instead of radiobutton, and just change the drawable onToggle.
You will need to create a custom radio button to change the style in that way. Read this article if you want to learn how to do it. Good luck.
No, you can't remove this.You can custom radio button.

Change android Ripple effect center position for android Appcompat radio button

I tried to make RTL radio -Put radio on right side- button for android with AppCompactRadioButton, and i achieved this with android:drawableRight and set android:button="#null" so my final view is what i wanted and final code is this:
<android.support.v7.widget.AppCompatRadioButton
android:id="#+id/radio_man"
style="#style/app_font"
android:layout_width="0dp"
android:layout_height="#dimen/edit_text_height"
android:layout_weight="1"
android:button="#null"
android:drawableRight="#drawable/form_radiobutton"
android:minHeight="33dp"
android:onClick="onSexRadioButtonClicked"
android:text="#string/hint_man" />
But my question is how to to change ripple effect center position, because i set button to null, the ripple effect center poisiton is in center of radio button like image above and its not expected by user to see ripple there. i tried to set layout_direction to RTL, but nothing fixed.
I have to mention that i also tried to fix this problem with ripple drawable like below but nothing changed and problem is still there.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/transparent">
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/btn_radio_activated"
android:state_checked="true" />
<item android:drawable="#drawable/btn_radio_deactive" />
</selector>
</item>
</ripple>
Long story short, is there any idea to change android Ripple effect center position for android Appcompat radio button?
I can succesfully erase the ripple effect by changing the background of the RadioButton to transparent like this:
android:background="#android:color/transparent"
I didn't yet tried but i guess you can set a drawable with left padding to overcome this problem.
I think what you're looking for is
android:background="?selectableItemBackground"
in your RadioButtons. That will overwrite the wrong ripple effect.

Check box material design

I'm using a check-box in my project, but I'm not able to eliminate the left padding, so the alignment isn't right.
Anyone know how to eliminate that left padding?
After some research I found out that, this specific margin is in the drawable of the check-square. So I suppose that the only solution is to create a custom drawable
This is because the drawable used by Android Checkbox has its own margin.
So to avoid it you''ll have to create your own checkbox with a custom drawable and icons for checked and unchecked states.
1> Create a custom selector for your checkbox like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/ic_unchecked" />
<item android:state_checked="true" android:drawable="#drawable/ic_checked" />
<item android:drawable="#drawable/ic_unchecked" /> <!-- default -->
</selector>
2>Then in your xml add a normal check box ,set drawable and button properties as null and assign your selector as the left drawable of your checkbox.Also give a drawable padding to give some padding between checkbox icon and text.
<CheckBox
android:id="#+id/chk_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:button="#null"
android:drawableLeft="#drawable/custom_checkbox"
android:drawablePadding="#dimen/margin_left_right"
android:text="#string/i_agree"
android:textSize="#dimen/text_size_small"
/>
NOTE ->
This soln also solve the padding problems of checkbox on and above android 4.2. which is also referred HERE

Android - change background of component while KEEPING selector?

Is this possible? I have for example simple linearLayout with selector to create "click effect":
<LinearLayout
android:id="#+id/clickToChangeColor"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/click_effect" />
This is selector click_effect:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/unpressed" android:state_enabled="false"/>
</selector>
Everything works just fine, but when I press on this layout, I would like to launch activity (like color picker - but it doesn't matter) and then change color of linearlayout for example to blue. BUT: keep selector "click effect". Trying to do this almost for 2 hours, but nothing works...
PS: I know it can be done for example with another layout inside this layout, apply some padding and apply selector to outer layout and then I can change background of inner layout etc - but it's only ugly workaround
<ImageView
android:id="#+id/imageViewSelectedColor"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/click_effect"
android:src="#color/picked color" />
Use an image view instead now you can change src according to picked color.

Remove default focus color from GridLayout and EditBox in Android

I want to remove the default orange focus color from GridView and from the EditText.
I have tried the android:focusable="false" but it is not working.
I think if this works as the other views, you have to use a selector in which you define various states for your view. A selector is a drawable (stored in the drawable folder) and you use it as if it was just an image. For instance, you could do something like that, if you want the focus to be red instead of orange :
selectorgridview.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#color/darkred" />
<item android:color="#color/white" />
</selector>
Then, you put the background of your GridView with it : android:background="selectorgridview"
I actually never tried it on a GridView but I think it works as the other views. More infos in the docs from google

Categories

Resources