I'm writing an API for Android and I need to know when the developer using the API calls View.setOnClickListener() and View.setOnFocusChangeListener(). I don't want to override either because that would mean to extend View and I don't want to force the developer to use my subclass -basically because he wouldn't be able to use the Android GUI editor for Eclipse-.
I tried to override Activity.dispatchTouchEvent() but then I cannot capture movements done with keypad/virtual keyboard.
Any ideas or guidelines on how to do this?
I found that using both Activity.dispatchTouchEvent() and Activity.dispatchKeyEvent() is the only way of doing this. You have to keep track of the view that had focus previously (so you can know when onFocusChange() is triggered). The onClick() can be detected using MotionEvent action and the view ID.
Related
I have an EditText which I have created a screen keyboard so I have deactivated the system keyboard. But the method I have underlines everything in yellow and gives me the following warning:
Custom view has setOnTouchListener called on it but does not override performClick If a View that overrides onTouchEvent or uses an OnTouchListener does not also implement performClick and call it when clicks are detected, the View may not handle accessibility actions properly. Logic handling the click actions should ideally be placed in View#performClick as some accessibility services invoke performClick when a click action should occur.
I know I could easily get rid of this error using the #SuppressLint("ClickableViewAccessibility") tag but I don't want to do it this way, I want to fix the warning in a more correct way, solutions?
The issue it's warning you about is that you've only handled the case of a user touching the screen, but there are many other ways of interacting with the EditText, such as keyboards or accessibility services.
For your specific use-case of preventing the keyboard from showing, you can instead use setShowSoftInputOnFocus(true) to prevent the soft keyboard from displaying. This method is available on API 21+, though some people have suggested that you may be able to get it to work through unofficial methods on earlier versions.
If you can't or don't want to do that, you should also override performClick and do the same thing you're doing in onTouchEvent, whatever that may be. If you do that, make sure that you test with a keyboard and/or accessibility service to make sure that your app is still usable for those users.
Simple scenario - ScrollView hosting a single LinearLayout which has several children - the preferred pattern to allow a scroll enabled views in developing on Android.
I need to capture onScrollEvents, start, stop, etc. that are readily available and work very well if I use ListView/onScrollListener combination.
A dedicated widget for scrolling a view DOES NOT have the built-in capabilities to capture these events? Come On! I have tried OnTouchEvent listener, onGestureListener, etc - all work arounds and all meant for something else. What am I missing here?
Unfortunately, there is no pre-built way for getting these events. However, there are two workarounds:
Use a ListView instead
Build your own custom ScrollView with ScrollListener. This is not that hard to achieve and has been done before.
Can anyone please help me in understanding drawableStateChanged method of toggle button in android??? I wanted to know why exactly it is used for and how to implement it??
I found it on following link:
http://developer.android.com/reference/android/widget/ToggleButton.html.
from reading through the documentation which states:
This function is called whenever the state of the view changes in
such a way that it impacts the state of drawables being shown.
it seems that this function will be called by the framework whenever the component needs to be redrawn and you can override it to (for example) perform so application specific logic which you need to do when the component is redrawn, like manually drawing something over the top of the component, or changing the font or doing something which is not possible using the stock attributes.
This question has an example of how you might implement it.
Helo,
Is their any library that supports the swipe to delete feature as implemented in gmail on Android, that also shows the undo button ? I saw this also on google io 2013, so i assumed this is natively supported by Android ? Is this so ?
Kind Regards
yes there is but you need to slightly modify those based on your need:
https://github.com/romannurik/Android-SwipeToDismiss and this https://github.com/timroes/SwipeToDismissUndoList
and this https://github.com/47deg/android-swipelistview
There is no library i guess. But u can implement it by using DragListener and making the view's visibility to gone (make a custom view by extending it). And when you undo, set the Visibility to "Visible"
You can also use OnTouchListener and listen to the co-ordinates on which user performs drag & then perform your logic.
On android we have onDraw(). What is the equivalent in iOS ?
You probably want drawRect:, though depending on what you want in your view there might be other options of interest (subviews & Core Animation layers). See the View Programming Guide.
If you're writing a custom view, it's basically -drawRect: which gets called on the view every time the system wants to redraw (e.g., every time the runloop turns and -setNeedsDisplay flag is set.
you override the -drawRect method of a UIView to do direct drawing to the screen. However this isn't commonly needed for lots of use cases. If you want, provide more detail about what you want to achieve. There may be a more iOS way.