- I’m interested in how to disable to change to click on another radiobutton?
- 1 Answer 1
- Not the answer you’re looking for? Browse other questions tagged java android or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Remove ripple effect from RadioButton, android?
- 5 Answers 5
- How to avoid multiple button click at same time in android?
- 16 Answers 16
- Cheap Solution:
- “More Modern” Approach:
- WHY “enabled(true/false)”?
- How to set OnClickListener on a RadioButton in Android?
- 9 Answers 9
- How to set button click effect in Android?
- 16 Answers 16
I’m interested in how to disable to change to click on another radiobutton?
How to disable radio buttons group after clicking on some radio button in Android activity ?
This is my XML code
And this is my Java Code
I’m interested in how to disable to change to click on another radiobutton?
1 Answer 1
If I’m understanding correctly, you want to disable the RadioGroup once a certain RadioButton is clicked. Just add an OnCheckedChangedListener and if the id of the RadioButton clicked is the right one, then disable the group by using setEnabled(false) . Of course, you can’t reenable the RadioGroup unless you have some other logic in your code that will do the re-enabling.
Edit: It seems that setEnabled() doesn’t work, so you should try changing your code so it loops through each RadioButton upon checking radio0 :
Not the answer you’re looking for? Browse other questions tagged java android or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Remove ripple effect from RadioButton, android?
I have custom styled a RadioButton for adding radiobutton image on the right side
but, the radiobutton’s ripple effect also enlarged. how can i remove ripple effect from RadioButton? and also how to customize ripple effect?
Thank you in advance.
5 Answers 5
Make RadioButton background to transparent
Setting colorControlHighlight and colorControlActivated to transparent color solved this issue for me:
On the «how to customize ripple effect» part, using Material Components library v1.1.0, the background is by default a RippleDrawable with its color set to a ColorStateList of 2 colors: A 26 alpha (#A1 in hex) colorControlActivated in enabled and checked state, and for the default state an alpha white or black depending on whether you have a light (#1F000000) or dark theme (#33FFFFFF). I retrieved this info simply by inspecting the background of a MaterialCheckBox in the debugger.
You can just override the color of the RippleDrawable however you wish using its setColor method. For example, if you want to only change the accent colored part of the ripple effect while using a DayNight theme, you can run this Kotlin code sample:
Источник
How to avoid multiple button click at same time in android?
I’m using two button in view. While clicking two button simultaneously it will goes to different activity at a time. How to avoid this?
I have tried like this, But its not working please save.
16 Answers 16
The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span). Example:
you can disable the multi-touch on your app by using this android:splitMotionEvents=»false» and android:windowEnableSplitTouch=»false» in your theme.
Cheap Solution:
You don’t have a correct separation of concerns (MVP or any flavor) so you put your code in your Activity/Fragment
If you can’t handle this the correct way, at least do not use non-deterministic solutions (like a timer).
Use the tools you already have, say you have this code:
Now… in a completely different place in your logic, like… for example, your onCreate or your onResume or anywhere where you know you want your button working again…
“More Modern” Approach:
- Do the same, but put the logic in your Presenter.
- Your presenter will decide if the “button” action has been triggered.
- Your presenter will tell its “view”: enableMyButton(); or disableMyButton() depending.
- Your View will do the right thing.
- you know… basic separation of concerns.
WHY “enabled(true/false)”?
Because it’s built in. Because the button will respect its state (and if you have a correct state list, it will change appearance for you, and because it will always be what you expect). Also, because it’s easier to test a presenter full of mocks, than a full activity that can grow forever in code.
if you are using kotlin then create extension fun as following:
Now on any view just call :
For Kotlin users
In your Activity or Fragment or anywhere
just add this one line condition
The simple way to do it in Kotlin is to use:
for any one using data-binding :
Here’s a class that debounce’s clicks for View and MenuItem .
*/ public static OnClickListener debounce(@NonNull OnClickListener onClickListener) < return new ClickEvent().wrapOnClickListener(onClickListener); >/** * Wraps the provided <@link OnMenuItemClickListener OnMenuItemClickListener>in a * that will prevent multiple rapid clicks from executing. *
* Usage: * */ public static OnMenuItemClickListener debounce(@NonNull OnMenuItemClickListener onClickListener) < return new ClickEvent().wrapOnClickListener(onClickListener); >/** * Allows the debounce to be shared between views to prevent multiple rapid clicks between views. *
Источник
How to set OnClickListener on a RadioButton in Android?
I have two RadioButton s inside a RadioGroup . I want to set OnClickListener on those RadioButton s. Depending on which RadioButton is clicked, I want to change the text of an EditText . How can I achieve this?
9 Answers 9
I’d think a better way is to use RadioGroup and set the listener on this to change and update the View accordingly (saves you having 2 or 3 or 4 etc listeners).
Hope this will help you.
The question was about Detecting which radio button is clicked, this is how you can get which button is clicked
You could also add listener from XML layout: android:onClick=»onRadioButtonClicked» in your tag.
Since this question isn’t specific to Java, I would like to add how you can do it in Kotlin:
Here radio_group_id is the assigned android:id of the concerned RadioGroup. To use it this way you would need to import kotlinx.android.synthetic.main.your_layout_name.* in your activity’s Kotlin file. Also note that in case the radioGroup lambda parameter is unused, it can be replaced with _ (an underscore) since Kotlin 1.1.
For Kotlin Here is added the lambda expression and Optimized the Code.
Hope this will help you. Thanks!
Just in case someone else was struggeling with the accepted answer:
There are different OnCheckedChangeListener-Interfaces. I added to first one to see if a CheckBox was changed.
When adding the snippet from Ricky I had errors:
Источник
How to set button click effect in Android?
In Android, when I set a background image to a button, I can not see any effect on it when it’s clicked.
I need to set some effect on the button, so the user can recognise that the button is clicked.
The button should be dark for a few seconds when it is clicked. How to do this?
16 Answers 16
This can be achieved by creating a drawable xml file containing a list of states for the button. So for example if you create a new xml file called «button.xml» with the following code:
To keep the background image with a darkened appearance on press, create a second xml file and call it gradient.xml with the following code:
In the xml of your button set the background to be the button xml e.g.
Hope this helps!
Edit: Changed the above code to show an image (YOURIMAGE) in the button as opposed to a block colour.
It is simpler when you have a lot of image buttons, and you don’t want to write xml-s for every button.
Create your AlphaAnimation Object that decides how much will be the fading effect of the button, then let it start in the onClickListener of your buttons
of course this is just a way, not the most preferred one, it’s just easier
You can simply use foreground for your View to achieve clickable effect:
For use with dark theme add also theme to your layout (to clickable effect be clear):
To make your item consistent with the system look and feel try referencing the system attribute android:attr/selectableItemBackground in your desired view’s background or foreground tag:
Use both attributes to get desired effect before/after API level 23 respectively.
Or using only one background image you can achive the click effect by using setOnTouchListener
And if you don’t want to use setOnTouchLister , the another way of achieving this is
For all the views
But for cardview which has elevation use
For Circular click effect as in toolbar
Also you need to set
This is the best solution I came up with taking hints from @Vinayak’s answer. All the other solutions have different drawbacks.
First of all create a function like this.
Explanation:
getConstantState().newDrawable() is used to clone the existing Drawable otherwise the same drawable will be used. Read more from here: Android: Cloning a drawable in order to make a StateListDrawable with filters
mutate() is used to make the Drawable clone not share its state with other instances of Drawable. Read more about it here: https://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()
Usage:
You can pass any type of View (Button, ImageButton, View etc) as the parameter to the function and they will get the click effect applied to them.
Источник