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.
Related
I set an ImageButton in a RelativeLayout and i want to show a Ripple effect when the button get clicked. As far as i know it have to be enabled by default but for some reason it doesn't show even if i set ?attr/selectableItemBackgroundBorderless or ?attr/selectableItemBackground as foreground or background (i used ?android:attr instead of ?attr too).
I tried everything, i made a custom Ripple effect drawable and setted it as foreground/background and it stills not working.
The app is API 21 > targeting 31, i don't know if it have anything to do with it but i using Material3.dark as main theme.
Anyone have an idea of what could be wrong here?
ImageButon:
<ImageButton
android:id="#+id/tb_music"
style="#style/top_bar_button"
android:layout_marginLeft="8dp"
android:src="#drawable/note" />
Style top_bar_button:
<style name="top_bar_button">
<item name="android:layout_width">64dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">#00000000</item>
<item name="android:foreground">#drawable/ripple_effect</item>
<item name="android:clickable">true</item>
</style>
ripple_effect.xml:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#FFFFFF">
<item android:id="#android:id/mask"
android:drawable="#android:color/white">
<shape android:shape="rectangle"> </shape>
</item>
</ripple>
How the button looks
don't use background attribute with ImageButton or Button. These widgets have 9-patch set as background, so when you overwrite it you basically lose default Androids button style and padding. Now you bascially may have in here ImageView or even TextView with drawable set to left/right
for coloring button (image or common, or even other Views) use android:tint attribute, but this will work with API23+ (so you may look for some compat/androidX version if you need API21, note it implements TintableBackgroundView, so we may assume that tint attribute will work, with custom not-android prefix)
also in question you should show your ripple effect drawable file, but even assuming that is is properly written - you have set transparent background, thus ripple effect won't be visible, as it works only in bounds of background - you transparent color removed default "shape" of this View. Just like elevation and translationZ params, these are adding some shadow under View, but only for not transparent, you've also lost this visual effect in here
so in short: remove background attribute leaving default one (or set it as non-transparent-at-all 6-hash solid color, but this will become just colored rectangle) and get familiar with tint and tintMode
aaand if you need a ripple effect on transparent rectange View then afaik thats possible also, but with mask param in drawables XML. and you don't need ImageButton, any View is clickable in Android, especially when set View.OnClickListener using Java/Kotlin. so ImageView will be sufficiet
I don't know why ripple does not work in your app but when I tested it, It is working properly. I think you didn't give padding to your ImageButton. You can see my XML code that how my code shows the Ripple effect.
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
app:srcCompat="#drawable/note"
tools:ignore="ContentDescription" />
<!-- Make sure that you have added padding to show ripple effect. Otherwise ripple effect not show-->
Let me know if your problem is not solved yet.
I have a AppCompatButton defined in a XML layout, and I have set a theme for it like this:
android:theme="#style/CustomAccentOverlay"
And I have set:
android:stateListAnimator="#null"
to remove the shadow. I have two problems with this.
The height of the button is deducted the height of the shadow, even though the shadow is not shown. Should I remove the shadow in some other way, or how do I fix this?
The button has rounded corners, and I want the corners to be sharp. I can not set a background on the button, because I want to keep the standard ripple effect and that goes away if I set a background (at least I don't know how to keep it if I set a background). I have tried setting
<item name="android:bottomLeftRadius">0dp</item>
and all the other corners to the CustomAccentOverlay theme and also its corresponding style, but it does not work. How can I set the corner radius to zero on my button?
Thank you
Søren
Use the following code for the Button.
<android.support.v7.widget.AppCompatButton
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Button"
android:stateListAnimator="#null"
android:elevation="0dp"
android:background="#android:color/darker_gray"
android:foreground="?attr/selectableItemBackground"
/>
I will explain the attributes.
android:elevation="0dp" and android:stateListAnimator="#null". No shadow for the button.
android:background . Set the desired color as background. It removes the rounded corners.
android:foreground="?attr/selectableItemBackground" . It gives the ripple effect when the button is pressed.
Update 1:
It seems like android:foreground attribute for View works from API 23. For below APIs, create a drawable with ripple in drawable-v21 folder and set it as background to the button,
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="NewApi"
android:color="#color/ripple_color">
<item android:drawable="#color/normal_state_button_background_color"/>
</ripple>
For pre Lollipop versions, create a drawable with selector in drawable folder with the same name.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_color"
android:state_pressed="true" />
<item android:drawable="#drawable/focused_color"
android:state_focused="true" />
<item android:drawable="#drawable/normal_color" />
</selector>
First question:How to remove shadow of a button?
Here is the answer:
Just add this attribute to your button
android:stateListAnimator="#null"
Second question: How to make the corner of button sharp without losing the standard ripple effect.
Here is the answer: But first you have to make two drawble file with same name but one for below api 21 and one for api > 21 because the ripple is only available only api > 21.So now I am showing how to create that.Read the following text carefully
Right click on the drawble folder and choose new and "Drawble resource file" and hit next then name the drawble whatever you like and press ok.Then again right click on the drawble folder and choose new and "Drawble resource file" and hit next and name the drawble exactly what you named the previous drawble folder but this time at the bottom you can see a section called "available qualifiers".Go to this section and at the very bottom you can see "Version",click it and then you can see a arrow icon at the right,click it then in the "Platform api level" add 21 and then press ok.And now if you expand drawble folder you can see two file of your created drawble file.Once for api that is below 21 and once for upper 21.Open drawble file that you have created and make sure you open that have "(v21)" at the last.Now delete everything from there and add the following code
<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="?attr/colorControlHighlight" xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="0dp"/>
<solid android:color="#D6D7D7"/>
</shape>
</item>
</ripple>
And add this attribute to your button
android:background="#drawable/youdrawblefilethatyouhavecreated"
And now if you run your application you can see that there is no shadow and your button has sharp corner and if you click the ripple shows up.
Lastly,your button look something like this
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button 1"
android:background="#drawable/yourcreatddrawblefile"
android:stateListAnimator="#null"/>
Hope this help!
It really sounds like you want to use a clickable TextView rather than a Button. TextView by default will not have shadow and has sharp corners and you can attach a click listener to it. Remember, Button is just a fancy TextView with a lot of visual add-ons to it, and it sounds like you want to remove a lot of it.
If you want to keep the ripple on the TextView and define your own background, set android:foreground="?attr/selectableItemBackground"
EDIT: Even though the other answer was marked accepted, I would still argue that the OP should use a TextView with a click listener and applying the ripple effects to that than using a Button. Clickable TextViews in this manner are exactly how the Google I/O app implements all of their flat buttons that meet Material Design spec.
Use this code
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorAccent"
android:text="#string/button" />
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
I have the same ripple effect on most of my image buttons and on most of them it looks like a circle when pressed but on some buttons it looks like an oval and I'm just not sure why it looks different. I realize the actual ripple shape is set as oval but why does it look like a circle on some other buttons. It could be that I am just not seeing it right since it happens so quickly.
Here is my ripple xml:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="?android:colorPrimary" />
</shape>
</item>
</ripple>
Thanks.
EDIT: Turned out to be an issue with me using layout_weight on the ImageButtons inside a LinearLayout. I removed the weight from the individual buttons and instead added spaces with a weight of one between each button like this:
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
Yes . I too had the same issues. The effect is purely dependent on the dimension of the object. for eg a 512*512 image gave me perfect circled ripple effect and a rectangular button on the bottom of the screen gave me oval/ellipsed effect. so i added a small square button behind that rectangular button and added effect to that squared button. when that button is pressed i call that square button to show up the effect.
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