Make your Android Views accessible with only one line of code
If you care about your app’s accessibility you probably have already faced the following error message while running your UI tests:
There was 1 accessibility error: View falls below the minimum recommended size for touch targets. Minimum touch target size is 48x48dp.Even if you are not running accessibility checks in tests you may want to comply to Google’s accessibility standards about the size of touch targets, which is defined in this guide:
There are multiple solutions to this problem, and the most obvious and easiest one is just to add some padding to the clickable element.
This works in most cases, but if you have a background or a border in your button this solution will compromise your design. There can be also many more exceptions where this is not ideal.
TouchDelegate to the rescue
Fortunately Google has a built in solution for this, and that is the TouchDelegate class. With the help of this you can specify the area of the parent view, where touches will be delegated to the given child view. This might be confusing at first, but let’s see some code and it will be crystal clear.
The code is really simple.
- Get the hit area of the button.
- Change it according to our requirements.
- Apply the
TouchDelegateto the parent View.
This is really nice, but every time we face this problem we would need to write a lot of code. We can create an extension function to simplify our life.
Creating an extension function
We could create an extension function for the View class from the above code with 4 parameters, where we specify how many pixels we want to add to each side. That would be already a pretty neat solution, but why not go even further?
Get Tamás Kozmér’s stories in your inbox
Join Medium for free to get updates from this writer.
The following extension function makes the View’s touch target accessible, without the need of any manual calculations of how many pixels we need to add.
The code with the comments is pretty self explanatory. We just need to calculate the width and the height of the view and adjust the touch target if needed.
As I promised at the beginning, here is how you can make your Views accessible with one line of code:
mySmallButton.accessibleTouchTarget()Limitations and additional notes
- If you want to expand the touch target of multiple Views in the same parent, this solution alone won’t work, because the call on the second View will overwrite the parent’s touch delegate in this line:
parentView?.touchDelegate = TouchDelegate(delegateArea, this). A simple fix for this is wrapping each View in a FrameLayout, so that would get the delegate instead of the original parent. - To make sure this really works, the parent should have enough space next to the View you want to expand. If your View is in the top left corner of the parent without any padding, the touch target can’t expand in that direction.
- Accessibility Scanner can detect and account for the use of
TouchDelegateonly when running on Android 10 and later. - You can also test this manually if you enable Pointer location in the Developer options. You can see exactly where you tapped with the help of this tool.
- Another bonus to this approach if you have touch feedback animation on your View it won’t show outside of it, even if the touch area is expanded.
Thanks for reading my article. I hope you found it useful, and it can help you make your users happier.
If you liked it share it with fellow Android developers. If you have any questions or suggestions leave a comment below.

