Skip to content

Instantly share code, notes, and snippets.

View anhoang241998's full-sized avatar
🏢
Android Developer

Nguyễn Hoàng Ân anhoang241998

🏢
Android Developer
View GitHub Profile
@jeffaburt
jeffaburt / CGFloat+PixelConversion.swift
Last active August 2, 2024 18:53
Converts pixels to points based on the screen scale
import UIKit
public extension CGFloat {
/**
Converts pixels to points based on the screen scale. For example, if you
call CGFloat(1).pixelsToPoints() on an @2x device, this method will return
0.5.
- parameter pixels: to be converted into points
@afollestad
afollestad / BroadcastReceiver.kt
Last active January 14, 2024 10:23
A Lifecycle components aware BroadcastReceiver DSL (Kotlin)
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.lifecycle.Lifecycle.Event.ON_DESTROY
import androidx.lifecycle.Lifecycle.Event.ON_START
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import android.content.BroadcastReceiver as StockReceiver
@vklachkov
vklachkov / lost_focus_on_misclick.kt
Created February 10, 2020 19:43
Removing focus from the input field automatically when you click in any other area
// Put it in your Activity and it'll apply to all EditTexts including those within fragments within that activity
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
val v = currentFocus
if (v is EditText) {
val outRect = Rect()
v.getGlobalVisibleRect(outRect)
if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
v.clearFocus()
val imm: InputMethodManager =