Skip to main content

Posts

Showing posts from March, 2017

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 :

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

Android Session for beginners

Some time back, I took a session on 'Getting Started with Android' in my previous company. When we are getting started with android, there's so much to grasp. There are so many different components and it's very important to introduce each one of them to just the right depth, otherwise the audience may get lost in the middle. So, it was quite a challenge to prepare the material.  I decided to introduce the following components in the session :  Activities Intents Services Broadcast Receiver Recycler View Storage in Android Async Task floating action button And  this  repository contains the demo code that I used in my session. Something had to be done to make it easy for the audience to take all this, so this is the approach that I followed :  The repository contains different branches, each corresponding to a particular functionality. So, if you want to see the code changes for a particular one, you can just see the diff between