Getting a solution that is simpler than what another person has posted, an example…

Hey everyone,

A little bit before, I had posted on getting to a better solution. If you haven’t read it yet, you can find it here: http://improvisedcode.com/2018/08/29/getting-a-solution-that-is-simpler-than-what-another-person-has-posted/

Today, I am going to focus on an example of that. I came across this problem: https://stackoverflow.com/questions/3554377/handling-click-events-on-a-drawable-within-an-edittext

The answer by aristo_sh works, sure. What I see that isn’t pretty however, is that this touch event will trigger anywhere, and it will be ignored outside of the drawable. And although the check will handle the job well, there’s another problem to deal with: justification!

And what we mean by justification is how your layout draws depending on the region and language. In the solution posted above, there is no justification. aristo_sh’s solution is just checking on the right side when touched. The edge case is, what will happen with Hebrew? The event will be triggered probably within the EditText instead of the drawable! Not good…

I’ve learned that dealing with justification by using start and end instead of left and right will absolutely work for every language. However, typing up “GetEnd” or “End” didn’t come up with any result. Therefore, I knew this solution wouldn’t work for me.

So what I did instead was programmatically setup a transparent view, with twice the dimensions of the drawable that gravitates towards the end of my TextInputLayout (and in case you didn’t know, TextInputLayout is a child of LinearLayout), then set the top gravity to the negative of twice the drawable’s height before adding the view. This was great since I didn’t have to wire up any events I didn’t need for my EditText. Instead, I just had my click event for the transparent view that sits on top of the drawable.

It worked, and it was a lot simpler than the original solution. Therefore, it is assured that across all languages, this solution will also work.

Now, why is this better?

First of all, there is no checking involved. Secondly, it is just wiring up a view, and not any weird or extra EditText events. Third, it’s just another view that gets added onto a ViewGroup. Lastly, it is much easier to understand, and the next developer will certainly be happier of this choice.

Now it’s known that Google will rewrite their APIs quite soon. From now, we’ll have to see how much simpler they’ll be doing things versus the codebase that we will have to work with now, but I’m sure that there are always improvements to be made.

Anyway, I just wanted to share my two cents of what can be better. Therefore, just keep trying to find out the right way to do certain things!

Until next time!

Brian.