Skip to content

Instantly share code, notes, and snippets.

View e4basil's full-sized avatar
🎯
Focusing

Basi e4basil

🎯
Focusing
View GitHub Profile
@e4basil
e4basil / Autofocus.kt
Last active July 3, 2024 12:06
Auto-focus after X seconds and focus on tap in cameraX :
previewView.afterMeasured {
val autoFocusPoint = SurfaceOrientedMeteringPointFactory(1f, 1f)
.createPoint(.5f, .5f)
try {
val autoFocusAction = FocusMeteringAction.Builder(
autoFocusPoint,
FocusMeteringAction.FLAG_AF
).apply {
//start auto-focusing after 2 seconds
setAutoCancelDuration(2, TimeUnit.SECONDS)
// Create a listener with a callback invoked when a gesture event has occurred
val listener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector): Boolean {
// Get the current camera zoom ratio
val currentZoomRatio: Float = cameraInfo.zoomRatio.value ?: 1F
// Get by how much the scale has changed due to the user's pinch gesture
val delta = detector.scaleFactor
// Update the camera's zoom ratio
@e4basil
e4basil / ScreenUtils.java
Created March 24, 2023 08:26 — forked from CharlesHarley/ScreenUtils.java
Android utility class for working with the device screen.
/* Copyright 2012 Charles Harley
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@e4basil
e4basil / SharedPreferences
Created March 21, 2023 08:31
Extension functions for getting and putting values in SharedPreferences:
inline fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T? {
val value = when (T::class) {
String::class -> getString(key, defaultValue as? String) as T?
Int::class -> getInt(key, defaultValue as? Int ?: -1) as T?
Long::class -> getLong(key, defaultValue as? Long ?: -1L) as T?
Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T?
Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T?
else -> throw IllegalArgumentException("Unsupported type: ${T::class.java}")
}
return value
import android.content.res.Resources
// Convert px to dp
val Int.dp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
//Convert dp to px
val Int.px: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
@e4basil
e4basil / ViewVisibilityExtensions.kt
Created March 20, 2023 04:09 — forked from akshaykalola28/ViewVisibilityExtensions.kt
Kotlin Extension functions for handling the view visibility in android
import android.view.View
fun View.gone() = run { visibility = View.GONE }
fun View.visible() = run { visibility = View.VISIBLE }
fun View.invisible() = run { visibility = View.INVISIBLE }
infix fun View.visibleIf(condition: Boolean) =
run { visibility = if (condition) View.VISIBLE else View.GONE }
@e4basil
e4basil / ToastExtensions.kt
Created March 20, 2023 04:09 — forked from akshaykalola28/ToastExtensions.kt
Kotlin extension functions for show toast with single function.
import android.app.Activity
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
fun Fragment.toast(message: String) {
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show()
}
fun Fragment.toast(@StringRes message: Int) {
@e4basil
e4basil / KeyboardVisibility.kt
Created March 20, 2023 04:08 — forked from akshaykalola28/KeyboardVisibility.kt
Keyboard visibility for android (activity and fragment)
import android.app.Activity
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
fun Activity.hideKeyboard() {
val imm: InputMethodManager =
getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val view = currentFocus ?: View(this)
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
@e4basil
e4basil / DateFormatterExtensions.kt
Created March 20, 2023 04:08 — forked from akshaykalola28/DateFormatterExtensions.kt
Kotlin extenstion for date format in android
import java.text.SimpleDateFormat
import java.util.*
fun String.toDate(format: String = "yyyy-MM-dd HH:mm:ss"): Date? {
val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
return dateFormatter.parse(this)
}
fun Date.toStringFormat(format: String = "yyyy-MM-dd HH:mm:ss"): String {
val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
@e4basil
e4basil / PrintToLogcat.kt
Created March 20, 2023 04:08 — forked from akshaykalola28/PrintToLogcat.kt
Extenstion function to print in the logcat with default and custom log tag.
import android.util.Log
fun Any?.printToLog(tag: String = "DEBUG_LOG") {
Log.d(tag, toString())
}