Skip to content

Instantly share code, notes, and snippets.

View akshaykalola28's full-sized avatar
😍
Love to Code

Akshay Kalola akshaykalola28

😍
Love to Code
View GitHub Profile
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()
@akshaykalola28
akshaykalola28 / CheckString.kt
Created March 1, 2023 13:19
Extension function to check the string is digit only, alphabetic only or alphanumeric only.
val String.isDigitOnly: Boolean
get() = matches(Regex("^\\d*\$"))
val String.isAlphabeticOnly: Boolean
get() = matches(Regex("^[a-zA-Z]*\$"))
val String.isAlphanumericOnly: Boolean
get() = matches(Regex("^[a-zA-Z\\d]*\$"))
@akshaykalola28
akshaykalola28 / PrintToLogcat.kt
Last active March 20, 2023 04:08
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())
}
@akshaykalola28
akshaykalola28 / ViewVisibilityExtensions.kt
Created March 2, 2023 04:45
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 }
@akshaykalola28
akshaykalola28 / ToastExtensions.kt
Last active March 20, 2023 04:09
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) {
@akshaykalola28
akshaykalola28 / SnackbarExtensions.kt
Created March 2, 2023 06:13
Kotlin extenstion for snackbar visibility for android view
import android.view.View
import androidx.annotation.StringRes
import com.google.android.material.snackbar.Snackbar
fun View.snackbar(message: String, duration: Int = Snackbar.LENGTH_LONG) {
Snackbar.make(this, message, duration).show()
}
fun View.snackbar(@StringRes message: Int, duration: Int = Snackbar.LENGTH_LONG) {
Snackbar.make(this, message, duration).show()
@akshaykalola28
akshaykalola28 / KeyboardVisibility.kt
Last active March 20, 2023 04:08
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)
@akshaykalola28
akshaykalola28 / IsNullExtension.kt
Created March 2, 2023 06:51
isNull Extenstion for Android developer
val Any?.isNull get() = this == null
@akshaykalola28
akshaykalola28 / IfNullExtension.kt
Created March 2, 2023 06:57
Extension function for Run block ifNull
fun Any?.ifNull(block: () -> Unit) = run {
if (this == null) {
block()
}
}
@akshaykalola28
akshaykalola28 / DateFormatterExtensions.kt
Last active March 20, 2023 04:08
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())