Skip to content

Instantly share code, notes, and snippets.

View truedem's full-sized avatar

Pavel Kuznetsov truedem

View GitHub Profile
@truedem
truedem / AutoResizingEditTextExt.kt
Last active October 8, 2022 13:54
Auto-resizing EditText font with callback
// to change text size in related fields (like "+" in front of phone numbers or quasi-hint text in the field below):
// binding.textEdit.autocorrectSize { size ->
// binding.phoneCountryPrefixTextField.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
// binding.hintBelowInputTextField.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
// }
// or just myEditText.autocorrectSize() somewhere in onViewCreated()
fun EditText.autocorrectSize(callback: ((Float) -> Unit)? = null) {
val originalTextSize = this.textSize
@truedem
truedem / EditTextAutoSizeFonts.kt
Created April 19, 2022 06:35
EditText extention to auto-resize fonts (+ in other fields) if the text is too long
package com.utils.texts
import android.graphics.Paint
import android.graphics.Rect
import android.text.Editable
import android.text.TextWatcher
import android.util.TypedValue
import android.widget.EditText
import kotlin.math.max
import kotlin.math.min
@truedem
truedem / StringUtils.kt
Created August 21, 2021 21:16
Kotlin extentions to check out if all words are presented in the string
// Usage:
// if (fullname.containsWords(searchString)) { doStuff() }
fun String.containsWords(str: String?): Boolean {
val words = str?.trim()?.lowercase()?.split("\\s".toRegex())?.toTypedArray()
return Arrays.stream(words).allMatch { s: CharSequence? -> this.lowercase().contains(s.toString().lowercase()!!) }
}
// Usage:
// val words = searchString.trim().split("\\s".toRegex()).toTypedArray()
@truedem
truedem / AppCfg.kt
Last active July 15, 2021 10:47
Universal Android Confirmation Dialog
object AppCfg {
const val CONFIRMATION_PARAM = "resultKey"
const val CONFIRMATION_TITLE = "title"
const val CONFIRMATION_TEXT = "content"
const val CONFIRMATION_POSITIVE = "positiveLabel"
const val CONFIRMATION_NEGATIVE = "negativeLabel"
const val CONFIRMATION_POSITIVE_ICON = "positiveIcon"
const val IS_DELETED = "IS_DELETED"
const val IS_EXPORTED = "IS_EXPORTED"
@truedem
truedem / VerticalSeekbar.kt
Created April 14, 2021 03:40
Vertical SeekBar for Android
class VerticalSeekBar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatSeekBar(context, attrs, defStyleAttr) {
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(h, w, oldh, oldw)
}
override fun setProgress(progress: Int) {
val oldWidth = width
@truedem
truedem / EditTextAsStateFlow.kt
Last active March 12, 2021 00:21
Extention function tu turn EditText input on Android into StateFlow
// source: https://github.com/HamdiBoumaiza/Stars/blob/main/app/src/main/java/com/hb/stars/utils/Extensions.kt
// extention function anywhere:
fun EditText.getTextChangeStateFlow(): StateFlow<String> {
val query = MutableStateFlow("")
addTextChangedListener {
query.value = it.toString()
}
return query
}
@truedem
truedem / ViewModelFactory.kt
Last active December 14, 2020 13:20
ViewModelFactory for Android Jetpack
class ViewModelFactory constructor(
private val baseRepository: BaseRepository, // your repository class to handle database, network, shared preferences, etc.
owner: SavedStateRegistryOwner,
defaultArgs: Bundle? = null
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
// usage in activity:
// val sharedVM by viewModels<MySharedViewModel> { ViewModelFactory(App.getRep(), this) }
// App.getRep() returns instance of BaseRepository, replace with your actual logic
@truedem
truedem / DayNightModeOn.kt
Created June 26, 2020 20:39
Kotlin one-liner to check if Android device is in day or night mode?
// usage in fragment: val color = if(requireContext().isDarkThemeOn()) Color.DKGRAY else Color.WHITE
// source: https://stackoverflow.com/questions/55787035/is-there-an-api-to-detect-which-theme-the-os-is-using-dark-or-light-or-other
fun Context.isDarkThemeOn() = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
@truedem
truedem / RecyclerViewExample.kt
Last active September 1, 2021 06:55
Save / restore recyclerview state for Android (after screen rotation) - example
private val ARGS_SCROLL_STATE = "recyclerState"
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable(ARGS_SCROLL_STATE, recyclerView.getLayoutManager()?.onSaveInstanceState())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@truedem
truedem / ClosestDistance.kt
Last active January 21, 2020 22:14
How close is the geo-location to the path?
// https://www.reddit.com/r/androiddev/comments/ert88w/asking_if_this_is_possible_to_do_with_maps_api/
fun closestDistance(point:LatLng, route:Polyline) : Double {
var minDist = Double.MAX_VALUE
for (i in 0 until (route.points?.size ?: 0) - 1) {
val dist=lineDistance(point,route.points[i],route.points[i+1])
if (dist<minDist){
minDist=dist
}