This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]*\$")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log | |
fun Any?.printToLog(tag: String = "DEBUG_LOG") { | |
Log.d(tag, toString()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val Any?.isNull get() = this == null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Any?.ifNull(block: () -> Unit) = run { | |
if (this == null) { | |
block() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
OlderNewer