Skip to main content

Android Tip : Touch Delegate

Android Tip : Touch Delegate

As android developer docs say about this class :

Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds.

You may require this in some obvious cases such as - when you want to keep the size of a view (let’s name it V) small to suit your VD, but that size is too small to be conveniently used.

The class is named touch delegate because we are explicitely delegating some extra touches to V which it won’t have gotten by default.

How you do it? :

First create an instance that takes the delegate view and the bounds that should be mapped to the delegate using the constructor :

public TouchDelegate(Rect bounds, View delegateView)

IMP : bounds here refers to local coordinates of the containing view that should be mapped to the delegate view.

It depends on your use case on how you calculate this.

For example, if you just want to increase the touch are of V in all directions, you could do something like :

                final Rect rect = new Rect();
                V.getHitRect(rect);
                rect.top -= 20;
                rect.bottom += 20;
                rect.left -= 20;
                rect.right += 20;
                TouchDelegate touchDelegate = new TouchDelegate(rect, superCardHelp));

Last step is setting this delegate on your container view (parent view of V). This can be done using by the setTouchDelegate, which essentially also means that you can set only a single touch delegate on the view.

If you go a little deep into how this works, the parentView checks in its onTouchEvent callback if a touch delegate is there. If it’s there, it calls the onTouchEvent on the delegate which returns true if this event was handled. (See the source code here). So, in case you want to override the default behaviour of TouchDelegate, you can extend this class and override the onTouchEvent method.

That’s it!

Let me know in case of any issues.


Comments

Popular posts from this blog

Android : AbsSavedState cannot be cast to $SavedState

Android AbsSavedState cannot be cast to $SavedState I came across a strange crash today. This is the stacktrace : Fatal Exception: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ScrollView$SavedState at android.widget.ScrollView.onRestoreInstanceState(ScrollView.java:1810) at android.view.View.dispatchRestoreInstanceState(View.java:14768) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3123) at android.view.View.restoreHierarchyState(View.java:14746) at android.support.v4.app.Fragment.restoreViewState(SourceFile:470) at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1326) at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(SourceFile:2323) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(SourceFile:2136) at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(SourceFile:2092) at androi...

Android Tip : Handling back button in Fragments

Android Tip : Handling hardware back button in Fragment and DialogFragment This post explains how to handle hardware back button in a Fragment OR DialogFragment . In DialogFragment, it’s quiet straight forward to achieve this. You’ve to get the dialog instance and set onKeyListener on it : if (getDialog() != null ) { getDialog().setOnKeyListener( new DialogInterface.OnKeyListener() { @Override public boolean onKey (DialogInterface dialog, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) { Timber.i( "hardware back button pressed" ); } } }); } This can be done in the onViewCreated callback. For fragments, this method doesn’t work and fragments doesn’t have a direct callback to catch this event. So in this case, the approach that we follow is : You...

Android Material Showcase View - Part 1

In this series, I'll be talking about a library which is used by a lot of android developers for showcasing their in-app content,  MaterialShowcaseView . I used this library sometime back for my work and had to modify it to fit my needs. In this process, I ended up digging it a lot and would like to share what I learned. The original library offers a fix set of features, which are demonstrated by the screenshots in the README. Let's jump on the technicalities right away. The original library offers two things : 1) highlighting a view ( see the README to know what I mean by highlighting here) 2) showing a content box that tells user what the highlighted view is about. Here's how it does this : The library adds a direct view child ( this class ) in the window decor view, so that it's drawn on top of your activity's view ( here ). Then, for drawing the overlay and highlighting our view, it overrides the `onDraw()` method of this view and uses android...