Grab Android Utilities that can help you everyday [part II]

I have introduced some common utilities in [part I] here are are some more that you can use everyday.

I have made them extension function on fragment , you can convert for activity if you want.

Easy Way to show a loader

Most of time I see developers adding a LottieAnimationView in all screen a setting it’s visibility for showing or removing loaders, do not do that because first it is duplicate code everywhere second the loader views always in memory, instead you can load it on demand and release when done.

just say showLoader(yourlayout) loader will be centered in the layout

then just say removeLoader(yourlayout) to remove this loader from memory

Enable and Disable Touch Easily

when showing loader you would not want user to click anything

just say enableTouch() or disableTouch() you can call the former in the showLoader(ViewGroup) and later in the removeLoader(ViewGroup) or in the fragment itself as your wish.

Do not write long animation code just to Translate a View with animation

just say yourView.translate(from,to) will translate with animation.

Do not write long animation code just to scale a view with animation

just say yourView.animateScale(100,200)

Do not hit Api for every character in a search view

you have an app with search feature you want to show results as the user is typing.

ex — user typing shoes, you are hitting api in ontextchanged() so api calls will be for s, sh , sho, shoe , shoes 5 apis calls in total not including when user mistypes and then press back spaces your apis call are ≥ number of letters in the search

Just hit it once by using below utility it will wait 1 second before hitting the api do not hit it for duplicate queries and do not hit for empty string find more details here

Do not write boilerplate for launching Coroutines

when launching a coroutine you are most of time doing lifecyclescope.lauch(Dispatchers.IO) and same for different dispatchers, just giving you a more readable short hand for this.

just say launchIO { // your code} , launchMain{ // your code}

Easily Convert Dp to Px

just day 5.toPx() that’s it

Hide soft keyboard just by calling a function

you have app app with search feature you want to hide keyboard as search begins just say hideSoftKeyboard()

Set a double click listener like a OnClickLisntener

You have an app with double tap like , just say yourimageView.doOnDoubleClick{ } and reuse every where.

thanks have a good day 😊

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store