I get this warning (from the question title) in a custom Android view I am developing.
Why do I get warned? What's the logic behind it i.e. why is it a good
practice to also override performClick
when you override onTouchEvent
?
I get this warning (from the question title) in a custom Android view I am developing.
Why do I get warned? What's the logic behind it i.e. why is it a good
practice to also override performClick
when you override onTouchEvent
?
This warning tells you to override
performClick
But it is not compulsory. As I have created a custom knobView and it is working quite good where I am also facing this warning.
The
onTouchEvent
is not called by some Accessibility services, as explained by clicking the "more..." link in the warning details.It recommends that you override
performClick
for your desired action, or at least override it alongside youronTouchEvent
.If your code is more fitting for the touch event, you can use something similar to:
More information on accessing touch events through code is available at trigger ontouch event programmatically
What's the purpose?
In some of the other answers you can see ways to make the warning go away, but it is important to understand why the system wants you to override
performClick()
in the first place.There are millions of blind people in the world. Maybe you don't normally think about them much, but you should. They use Android, too. "How?" you might ask. One important way is through the TalkBack app. It is a screen reader that gives audio feedback. You can turn it on in your phone by going to Settings > Accessibility > TalkBack. Go through the tutorial there. It is really interesting. Now try to use your app with your eyes closed. You'll probably find that your app is extremely annoying at best and completely broken at worst. That's a fail for you and a quick uninstall by anyone's who's visually impaired.
Watch this excellent video by Google for an introduction into making your app accessible.
How to override
performClick()
Let's look at a example custom view to see how overriding
performClick()
actually works. We'll make a simple missile launching app. The custom view will be the button to fire it.It sounds a lot better with TalkBack enabled, but animated gifs don't allow audio, so you will just have to try it yourself.
Code
activity_main.xml
Notice that I set the
contentDescription
. This allows TalkBack to read out what the custom view is when the user feels over it.CustomView.java
Notes
mDownTouch
variable which appears to be used to filter out extra touch up events, but since it isn't well explained or strictly necessary for our app, I left it out. If you make a real missile launcher app, I suggest you look more into this.launchMissile()
) is just called fromperformClick()
. Be careful not to call it twice if you also have it inonTouchEvent
. You will need to decide exactly how and when to call your business logic method depending on the specifics of your custom view.Don't override
performClick()
and then do nothing with it just to get rid of the warning. If you want to ignore the millions of blind people in the world, then you can suppress the warning. At least that way you are honest about your heartlessness.Further study