Skip to content

Instantly share code, notes, and snippets.

View tizisdeepan's full-sized avatar
🎯
Focusing

Deepan tizisdeepan

🎯
Focusing
View GitHub Profile
@tizisdeepan
tizisdeepan / RunOnUiThread.kt
Last active April 22, 2019 06:41
A simple Kotlin class to run a function safely on a UiThread anywhere anytime without crashing the application. Avoids crashes, ANRs and logs the problem efficiently.
import android.content.Context
import org.jetbrains.anko.runOnUiThread
class RunOnUiThread(var context: Context?) {
fun safely(dothis: () -> Unit) {
if (context != null) {
context?.runOnUiThread {
try {
dothis.invoke()
} catch (e: Exception) {
@tizisdeepan
tizisdeepan / FontsHelper.kt
Created April 22, 2019 06:36
A Fonts Cache mechanism that can be used in your project right away. Do you know that creating a typeface object from resources every time you set a font to your views is memory expensive? Use this simple helper class to avoid such memory leaks and improve your app's performance.
import android.content.Context
import android.graphics.Typeface
import com.zoho.zohosocial.utils.MLog
import java.util.*
object FontsHelper {
private val TAG = "TypefaceHelper"
private val fontsCache = Hashtable<String, Typeface>()
@tizisdeepan
tizisdeepan / HideKeypad.kt
Last active April 22, 2019 06:45
A class to hide the keypad that has been opened in any screen. It's simple to use and does the job.
import android.app.Activity
import android.content.Context
import androidx.fragment.app.FragmentActivity
import android.view.inputmethod.InputMethodManager
class HideKeypad {
fun now(activity: Any?) {
if (activity != null) {
if (activity is Activity) {
val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
@tizisdeepan
tizisdeepan / PreferencesManager.kt
Created April 22, 2019 06:49
A Kotlin class that can handle Preferences easily by enabling clean code in your project.
import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
object PreferencesManager {
fun defaultPrefs(context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
fun customPrefs(context: Context, name: String): SharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE)
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
@tizisdeepan
tizisdeepan / LockableViewPager.kt
Created April 22, 2019 08:26
A ViewPager component that disables swipe navigation.
import android.content.Context
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import android.util.AttributeSet
class LockableViewPager : ViewPager {
var swipeLocked: Boolean = true
constructor(context: Context) : super(context)
@tizisdeepan
tizisdeepan / AnimationlessViewpager.kt
Created April 22, 2019 08:33
A ViewPager component that has no animation while switching between pages.
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
class AnimationlessViewpager : androidx.viewpager.widget.ViewPager {
var swipeLocked: Boolean = true
constructor(context: Context) : super(context)
@tizisdeepan
tizisdeepan / ClipBoardManager.kt
Last active April 22, 2019 08:42
A Clip Board utility for copying contents to Clip Board.
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.widget.Toast
object ClipBoardManager {
fun copy(ctx: Context, text: String, message: String = "") {
val myClipboard = ctx.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val myClip = ClipData.newPlainText(message, text)
myClipboard.primaryClip = myClip
@tizisdeepan
tizisdeepan / NoGifEditText.kt
Created April 22, 2019 08:39
EditText that ignores GIF support from Google Keypad.
import android.content.Context
import androidx.core.view.inputmethod.EditorInfoCompat
import androidx.core.view.inputmethod.InputConnectionCompat
import androidx.appcompat.widget.AppCompatEditText
import android.util.AttributeSet
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.widget.Toast
import com.zoho.zohosocial.utils.RunOnUiThread
@tizisdeepan
tizisdeepan / NetworkUtil.kt
Created April 22, 2019 08:41
Network utility class that helps in identifying whether there is a stable internet connection or not.
import android.content.Context
import android.net.ConnectivityManager
object NetworkUtil {
fun isConnected(context: Context): Boolean {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = cm.activeNetworkInfo
return activeNetwork != null && activeNetwork.isConnectedOrConnecting
}
}
@tizisdeepan
tizisdeepan / SquareLayout.kt
Created April 22, 2019 12:37
SquareLayout makes sure that the view appears to be a square at all times no matter what it's attributes and children are.
import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
class SquareLayout : FrameLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)