I have a Switch, or a SwitchCompat rather on my app.
When the user clicks or slides the switch I would like to run some code to determine if they should be allowed to.
I have tried the
setOnCheckedChangeListener and setOnClickListener methods but both allow the graphic of the slider to toggle before running the validation code.
How can I run my own code before anything else when the switch is pressed?
Thanks
you should write your validation code first. if validation is false they not allowed to press switch using setEnabled(false) the else user is allowed to press switch
We have three options
One you have tried by setOnCheckedChangeListener()
Other is to disable the view entirely by using setEnabled()
You may try using setOnTouchListener and write your validation code there, and if the user is not allowed to do the functionality just return true else return false
Returning true would make the Switch believe the touch was handled and it won't do any actions for the same
Related
So I am currently working on a scanner function and I need to read out a chain of key inputs.
The problem now is that whenever the scanner has completed scanning a code it presses Enter.
That in return triggers a click event on the currently focused item.
Is there any way to stop that from happening? So far I didn't find any information whatsoever on how to stop that from happening. Any help is much appreciated!
The easiest way would be to add a key listener that does nothing:
view.setOnKeyListener { v, keyCode, event ->
true
}
You return true meaning that you handled the event, while actually didn't do anything.
I'm building a settings screen. Some features should be locked for the free user, and when he click on that preference, a toast message tells him that this is a PRO only feature.
If I try preference.setEbabled(false) then try to implement an onClick listener, nothing happens and the item remains just disabled.
I don't want to add a null change listener, as in ListPreference it will show the selection dialog, which I don't want.
How to manage that?
You can try something with .setActivated(false) instead of .setEnabled(false). In your OnClick you can check if activated or not with .isActivated() and proceed based on the return value.
Kindly check this link , in this on click of cart button ,it shows animation that its added to cart , but i want to keep a validation over this button ,then only it should get added to the cart . Please help me with this code
https://github.com/truizlop/FABRevealLayout
You've got two possibilities:
1) Override the clickListener from the library by registering your own at the FloatingActionButton using fabButton.setOnClickListener(myListener). Make sure you're doing that after the fabRevealLayout has been inflated and initialized. In onCreate() after calling through to super.onCreate() should be fine.
Inside your own clickListener, you perform your validation and based on the result manually trigger the revealing of what the library considers the "secondary view" (fabRevealLayout.revealSecondaryView();) or do not.
2) Register a onTouch listener to the fab button. Inside that touch-listener you check whether it is an ACTION_DOWN event and then do your validation. If it succeeds, return true to notify the system, that the event has not been handled by your functionality (that it has "consumed" the event). Downside: No click sound when pressing among others, so "not the nice way", as we're interfering the android touch handling. On the other hand you do not have to manipulate the third party library.
I would recommend going with the first option.
I want to be able to press a button on my program and hold it down (without releasing) to increment a variable. Problem I am having right now is when I conduct the long button press it only runs once, until I release and press again.
First I want to find out if there is a way to do this without having to use the OnTouchListener, and just using the OnLongClick. Is there a way to check the value of the button? For example.. buttondown=true; Conduct a whileloop to increment until the button is released.
Second, I don't want the updates to be delayed, because the incremented value is being drawn as the user holds down the button.
Basically I am doing something like this:
btn_resume.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
..code..
return true;
}
});
OnLongClick will only be called once per press. It isn't going to work for your purpose.
If I understood your question correct this can be achieved using a OnLongClickListener.
Check http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)
OnTouchListener provides a more granular handling of touch events, e.g. KeyDown, KeyUp
I think you can use OnLongClickListener for increment/decrement. But once the long press is done for the button, the longpress has to be canceled or reset for the next long press of the same button.
I show to user a list of categories, he must choose one.
How to force the user to choose before pressing back?
Assuming you're asking the user to choose via a Dialog, use the setCancelable(false) flag. According to the documentation, that prevents the use of the back button.
Don't do this. Just handle the case when user doesn't select anything.
You could probably override the handling of the back button, but the more correct thing to do is simply handle the cancellation. In your onActionResult override, only react if the result argument is Activity.RESULT_OK (it will be Activity.RESULT_CANCELED if the user pushes the back button).
I would use a spinner and preselect a good default value.
Forcing the user to do something should be only the last resort in your application.