Skip to main content

Posts

Showing posts with the label tips

DialogFragment : NullPointerException (support library)

Another weird crash this time ! Here’s the stack trace : Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{<activity.fully.qualified.path>}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Dialog.setContentView(android.view.View)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4524) at android.app.ActivityThread.-wrap19(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1479) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6123) at java.lang.reflect.Method.invoke(Method.java) at com.android...

Android Tip : ApplicationInfo

This one is a pretty small but useful tip. For people who don’t know about it, Android provides a class by the name ApplicationInfo . As stated by the docs, this class provides information about your application and it collected from the Android Manifest’s <application> tag. It might not sound useful right now, but may come handy for some use cases : Data directory for your app : Checking if the app is debuggable or not Name of the process of your application Default Task Affinity of all the activities in the application Full path to the base apk of the application Let me know in case of any doubts. Cheers !

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...