Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ravidsrk/kotlinextensions.com
A handy collection of most commonly used Kotlin extensions to boost your productivity.
https://github.com/ravidsrk/kotlinextensions.com
android fragments kotlin-extensions textview viewgroup
Last synced: 4 days ago
JSON representation
A handy collection of most commonly used Kotlin extensions to boost your productivity.
- Host: GitHub
- URL: https://github.com/ravidsrk/kotlinextensions.com
- Owner: ravidsrk
- License: mit
- Created: 2017-08-30T03:13:06.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-19T13:00:27.000Z (about 4 years ago)
- Last Synced: 2024-08-01T16:44:25.550Z (4 months ago)
- Topics: android, fragments, kotlin-extensions, textview, viewgroup
- Language: Kotlin
- Homepage: http://kotlinextensions.com/
- Size: 57.6 KB
- Stars: 592
- Watchers: 26
- Forks: 98
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Kotlin Extensions
> Curated list of Most commonly used Kotlin Extensions.
- [View](#view)
- [Context](#context)
- [Fragment](#fragment)
- [Activity](#activity)
- [ViewGroup](#viewgroup)
- [TextView](#textview)
- [String](#string)
- [Other](#other)## View
```kotlin
/**
* Extension method to provide simpler access to {@link View#getResources()#getString(int)}.
*/
fun View.getString(stringResId: Int): String = resources.getString(stringResId)
``````kotlin
/**
* Extension method to show a keyboard for View.
*/
fun View.showKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
this.requestFocus()
imm.showSoftInput(this, 0)
}
``````kotlin
/**
* Try to hide the keyboard and returns whether it worked
* https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard
*/
fun View.hideKeyboard(): Boolean {
try {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
return inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
} catch (ignored: RuntimeException) { }
return false
}
``````kotlin
/**
* Extension method to remove the required boilerplate for running code after a view has been
* inflated and measured.
*
* @author Antonio Leiva
* @see Unit) = activity.notification(body)
``````kotlin
/**
* Extension method to display notification text for SupportFragment.
*/
inline fun SupportFragment.notification(body: NotificationCompat.Builder.() -> Unit) = activity.notification(body)
``````kotlin
/**
* Extension method to browse url text for Fragment.
*/
fun Fragment.browse(url: String, newTask: Boolean = false) = activity.browse(url, newTask)
``````kotlin
/**
* Extension method to browse url text for SupportFragment.
*/
fun SupportFragment.browse(url: String, newTask: Boolean = false) = activity.browse(url, newTask)
``````kotlin
/**
* Extension method to share text for Fragment.
*/
fun Fragment.share(text: String, subject: String = "") = activity.share(text, subject)
``````kotlin
/**
* Extension method to share text for SupportFragment.
*/
fun SupportFragment.share(text: String, subject: String = "") = activity.share(text, subject)
``````kotlin
/**
* Extension method to send email for Fragment.
*/
fun Fragment.email(email: String, subject: String = "", text: String = "") = activity.email(email, subject, text)
``````kotlin
/**
* Extension method to send email for SupportFragment.
*/
fun SupportFragment.email(email: String, subject: String = "", text: String = "") = activity.email(email, subject, text)
``````kotlin
/**
* Extension method to make call for Fragment.
*/
fun Fragment.makeCall(number: String) = activity.makeCall(number)
``````kotlin
/**
* Extension method to make call for SupportFragment.
*/
fun SupportFragment.makeCall(number: String) = activity.makeCall(number)
``````kotlin
/**
* Extension method to send sms for Fragment.
*/
fun Fragment.sendSms(number: String, text: String = "") = activity.sendSms(number, text)
``````kotlin
/**
* Extension method to send sms for SupportFragment.
*/
fun SupportFragment.sendSms(number: String, text: String = "") = activity.sendSms(number, text)
``````kotlin
/**
* Extension method to rate in playstore for Fragment.
*/
fun Fragment.rate() = activity.rate()
``````kotlin
/**
* Extension method to rate in playstore for SupportFragment.
*/
fun SupportFragment.rate() = activity.rate()
``````kotlin
/**
* Extension method to provide hide keyboard for [Fragment].
*/
fun Fragment.hideSoftKeyboard() {
activity?.hideSoftKeyboard()
}
```## Activity
```kotlin
/**
* Extension method to set Status Bar Color and Status Bar Icon Color Type(dark/light)
*/
enum class StatusIconColorType {
Dark, Light
}
fun Activity.setStatusBarColor(color: Int, iconColorType: StatusIconColorType = Light) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.window.apply {
clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
statusBarColor = color
decorView.systemUiVisibility = when (iconColorType) {
Dark -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
Light -> 0
}
}
} else
this.window.statusBarColor = color
}
``````kotlin
/**
* Extension method to provide hide keyboard for [Activity].
*/
fun Activity.hideSoftKeyboard() {
if (currentFocus != null) {
val inputMethodManager = getSystemService(Context
.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
}
``````kotlin
/**
* The `fragment` is added to the container view with id `frameId`. The operation is
* performed by the `fragmentManager`.
*/
fun AppCompatActivity.replaceFragmentInActivity(fragment: Fragment, @IdRes frameId: Int) {
supportFragmentManager.transact {
replace(frameId, fragment)
}
}
``````kotlin
/**
* The `fragment` is added to the container view with tag. The operation is
* performed by the `fragmentManager`.
*/
fun AppCompatActivity.addFragmentToActivity(fragment: Fragment, tag: String) {
supportFragmentManager.transact {
add(fragment, tag)
}
}
``````kotlin
/**
* Setup actionbar
*/
fun AppCompatActivity.setupActionBar(@IdRes toolbarId: Int, action: ActionBar.() -> Unit) {
setSupportActionBar(findViewById(toolbarId))
supportActionBar?.run {
action()
}
}
``````kotlin
/**
* Extension method to get ContentView for ViewGroup.
*/
fun Activity.getContentView(): ViewGroup {
return this.findViewById(android.R.id.content) as ViewGroup
}
```## ViewGroup
```kotlin
/**
* Extension method to inflate layout for ViewGroup.
*/
fun ViewGroup.inflate(layoutRes: Int): View {
return LayoutInflater.from(context).inflate(layoutRes, this, false)
}
``````kotlin
/**
* Extension method to get views by tag for ViewGroup.
*/
fun ViewGroup.getViewsByTag(tag: String): ArrayList {
val views = ArrayList()
val childCount = childCount
for (i in 0..childCount - 1) {
val child = getChildAt(i)
if (child is ViewGroup) {
views.addAll(child.getViewsByTag(tag))
}val tagObj = child.tag
if (tagObj != null && tagObj == tag) {
views.add(child)
}}
return views
}
``````kotlin
/**
* Extension method to remove views by tag ViewGroup.
*/
fun ViewGroup.removeViewsByTag(tag: String) {
for (i in 0..childCount - 1) {
val child = getChildAt(i)
if (child is ViewGroup) {
child.removeViewsByTag(tag)
}if (child.tag == tag) {
removeView(child)
}
}
}
``````kotlin
/**
* Extension method to simplify view inflating and binding inside a [ViewGroup].
*
* e.g.
* This:
*
* binding = bind(R.layout.widget_card)
*
*
* Will replace this:
*
* binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.widget_card, this, true)
*
*/
fun ViewGroup.bind(layoutId: Int): T {
return DataBindingUtil.inflate(getLayoutInflater(), layoutId, this, true)
}
```## TextView
[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method underLine for TextView.
*/
fun TextView.underLine() {
paint.flags = paint.flags or Paint.UNDERLINE_TEXT_FLAG
paint.isAntiAlias = true
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method deleteLine for TextView.
*/
fun TextView.deleteLine() {
paint.flags = paint.flags or Paint.STRIKE_THRU_TEXT_FLAG
paint.isAntiAlias = true
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method bold for TextView.
*/
fun TextView.bold() {
paint.isFakeBoldText = true
paint.isAntiAlias = true
}
``````kotlin
/**
* Extension method to set different color for substring TextView.
*/
fun TextView.setColorOfSubstring(substring: String, color: Int) {
try {
val spannable = android.text.SpannableString(text)
val start = text.indexOf(substring)
spannable.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, color)), start, start + substring.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
text = spannable
} catch (e: Exception) {
Log.d("ViewExtensions", "exception in setColorOfSubstring, text=$text, substring=$substring", e)
}
}
``````kotlin
/**
* Extension method to set font for TextView.
*/
fun TextView.font(font: String) {
typeface = Typeface.createFromAsset(context.assets, "fonts/$font.ttf")
}
``````kotlin
/**
* Extension method to set a drawable to the left of a TextView.
*/
fun TextView.setDrawableLeft(drawable: Int) {
this.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0)
}
```## String
[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to show toast for String.
*/
fun String.toast(isShortToast: Boolean = true) = toast(this, isShortToast)
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get md5 string.
*/
fun String.md5() = encrypt(this, "MD5")
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get sha1 string.
*/
fun String.sha1() = encrypt(this, "SHA-1")
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to check if String is Phone Number.
*/
fun String.isPhone(): Boolean {
val p = "^1([34578])\\d{9}\$".toRegex()
return matches(p)
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to check if String is Email.
*/
fun String.isEmail(): Boolean {
val p = "^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)\$".toRegex()
return matches(p)
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to check if String is Number.
*/
fun String.isNumeric(): Boolean {
val p = "^[0-9]+$".toRegex()
return matches(p)
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to check String equalsIgnoreCase
*/
fun String.equalsIgnoreCase(other: String) = this.toLowerCase().contentEquals(other.toLowerCase())
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get encrypted string.
*/
private fun encrypt(string: String?, type: String): String {
if (string.isNullOrEmpty()) {
return ""
}
val md5: MessageDigest
return try {
md5 = MessageDigest.getInstance(type)
val bytes = md5.digest(string!!.toByteArray())
bytes2Hex(bytes)
} catch (e: NoSuchAlgorithmException) {
""
}
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to convert byteArray to String.
*/
private fun bytes2Hex(bts: ByteArray): String {
var des = ""
var tmp: String
for (i in bts.indices) {
tmp = Integer.toHexString(bts[i].toInt() and 0xFF)
if (tmp.length == 1) {
des += "0"
}
des += tmp
}
return des
}
```## Other
```kotlin
/**
* Extension method to cast a char with a decimal value to an [Int].
*/
fun Char.decimalValue(): Int {
if (!isDigit())
throw IllegalArgumentException("Out of range")
return this.toInt() - '0'.toInt()
}
``````kotlin
/**
* Extension method to simplify the code needed to apply spans on a specific sub string.
*/
inline fun SpannableStringBuilder.withSpan(vararg spans: Any, action: SpannableStringBuilder.() -> Unit):
SpannableStringBuilder {
val from = length
action()for (span in spans) {
setSpan(span, from, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}return this
}
``````kotlin
/**
* Extension method to int time to 2 digit String
*/
fun Int.twoDigitTime() = if (this < 10) "0" + toString() else toString()```
```kotlin
/**
* Extension method to replace all text inside an [Editable] with the specified [newValue].
*/
fun Editable.replaceAll(newValue: String) {
replace(0, length, newValue)
}
``````kotlin
/**
* Extension method to replace all text inside an [Editable] with the specified [newValue] while
* ignoring any [android.text.InputFilter] set on the [Editable].
*/
fun Editable.replaceAllIgnoreFilters(newValue: String) {
val currentFilters = filters
filters = emptyArray()
replaceAll(newValue)
filters = currentFilters
}
``````kotlin
/**
* Extension method to get Date for String with specified format.
*/
fun String.dateInFormat(format: String): Date? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
var parsedDate: Date? = null
try {
parsedDate = dateFormat.parse(this)
} catch (ignored: ParseException) {
ignored.printStackTrace()
}
return parsedDate
}
``````kotlin
/**
* Extension method to get ClickableSpan.
* e.g.
* val loginLink = getClickableSpan(context.getColorCompat(R.color.colorAccent), { })
*/
fun getClickableSpan(color: Int, action: (view: View) -> Unit): ClickableSpan {
return object : ClickableSpan() {
override fun onClick(view: View) {
action(view)
}override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.color = color
}
}
}
``````kotlin
/**
* Extension method to load imageView from url.
*/
fun ImageView.loadFromUrl(imageUrl: String) {
Glide.with(this).load(imageUrl).into(this)
}
``````kotlin
/**
* Extension method to load icon from url.
*/
fun MenuItem.loadIconFromUrl(context: Context, imageUrl: String) {
Glide.with(context).asBitmap()
.load(imageUrl)
.into(object : SimpleTarget(100, 100) {
override fun onResourceReady(resource: Bitmap?, transition: Transition?) {
val circularIcon = RoundedBitmapDrawableFactory.create(context.resources, resource)
circularIcon.isCircular = true
icon = circularIcon
}
})
}
``````kotlin
/**
* Extension method to write preferences.
*/
inline fun SharedPreferences.edit(preferApply: Boolean = false, f: SharedPreferences.Editor.() -> Unit) {
val editor = edit()
editor.f()
if (preferApply) editor.apply() else editor.commit()
}
``````kotlin
/**
* Runs a FragmentTransaction, then calls commit().
*/
private inline fun FragmentManager.transact(action: FragmentTransaction.() -> Unit) {
beginTransaction().apply {
action()
}.commit()
}
``````kotlin
/**
* Get view model for Activity
*/
fun AppCompatActivity.obtainViewModel(viewModelClass: Class) =
ViewModelProviders.of(this, ViewModelFactory.getInstance(application)).get(viewModelClass)
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get value from EditText.
*/
val EditText.value
get() = text.toString()
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to check is aboveApi.
*/
inline fun aboveApi(api: Int, included: Boolean = false, block: () -> Unit) {
if (Build.VERSION.SDK_INT > if (included) api - 1 else api) {
block()
}
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to check is belowApi.
*/
inline fun belowApi(api: Int, included: Boolean = false, block: () -> Unit) {
if (Build.VERSION.SDK_INT < if (included) api + 1 else api) {
block()
}
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get base64 string for Bitmap.
*/
fun Bitmap.toBase64(): String {
var result = ""
val baos = ByteArrayOutputStream()
try {
compress(Bitmap.CompressFormat.JPEG, 100, baos)
baos.flush()
baos.close()
val bitmapBytes = baos.toByteArray()
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT)
} catch (e: IOException) {
e.printStackTrace()
} finally {
try {
baos.flush()
baos.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
return result
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to resize Bitmap to specified height and width.
*/
fun Bitmap.resize(w: Number, h: Number): Bitmap {
val width = width
val height = height
val scaleWidth = w.toFloat() / width
val scaleHeight = h.toFloat() / height
val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)
if (width > 0 && height > 0) {
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}
return this
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to save Bitmap to specified file path.
*/
fun Bitmap.saveFile(path: String) {
val f = File(path)
if (!f.exists()) {
f.createNewFile()
}
val stream = FileOutputStream(f)
compress(Bitmap.CompressFormat.PNG, 100, stream)
stream.flush()
stream.close()
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to find color based on color resource.
*/
fun findColor(@ColorRes resId: Int) = ContextCompat.getColor(Ext.ctx, resId)
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to find drawable based on drawable resource.
*/
fun findDrawable(@DrawableRes resId: Int): Drawable? = ContextCompat.getDrawable(Ext.ctx, resId)
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to find ColorStateList.
*/
fun findColorStateList(@ColorRes resId: Int): ColorStateList? = ContextCompat.getColorStateList(Ext.ctx, resId)
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to layout based on Layout Resource Id aot the parent ViewGroup
*/
fun inflate(@LayoutRes layoutId: Int, parent: ViewGroup?, attachToRoot: Boolean = false) = LayoutInflater.from(Ext.ctx).inflate(layoutId, parent, attachToRoot)!!
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to layout based on Layout Resource Id.
*/
fun inflate(@LayoutRes layoutId: Int) = inflate(layoutId, null)
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method check if is Main Thread.
*/
fun isMainThread(): Boolean = Looper.myLooper() == Looper.getMainLooper()
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to provide Date related functions.
*/
private enum class DateExpr {
YEAR, MONTH, DAY,
HOUR, MINUTE, SECOND,
WEEK, DAY_YEAR, WEEK_YEAR,
CONSTELLATION
}fun Long.date(pattern: String = "yyyy-MM-dd HH:mm:ss"): String? = SimpleDateFormat(pattern, Locale.getDefault()).format(this)
fun Long.year() = getData(this, DateExpr.YEAR)
fun Long.month() = getData(this, DateExpr.MONTH)
fun Long.day() = getData(this, DateExpr.DAY)
fun Long.week() = getData(this, DateExpr.WEEK)
fun Long.hour() = getData(this, DateExpr.HOUR)
fun Long.minute() = getData(this, DateExpr.MINUTE)
fun Long.second() = getData(this, DateExpr.SECOND)
fun Long.dayOfYear() = getData(this, DateExpr.DAY_YEAR)
fun Long.weekOfYear() = getData(this, DateExpr.WEEK_YEAR)
fun Long.constellation() = getData(this, DateExpr.CONSTELLATION)
fun Int.isLeapYear() = (this % 4 == 0) && (this % 100 != 0) || (this % 400 == 0)
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get connectivityManager for Context.
*/
inline val connectivityManager: ConnectivityManager
get() = Ext.ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get alarmManager for Context.
*/
inline val alarmManager: AlarmManager
get() = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get telephonyManager for Context.
*/
inline val telephonyManager: TelephonyManager
get() = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get activityManager for Context.
*/
inline val activityManager: ActivityManager
get() = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get notificationManager for Context.
*/
inline val notificationManager: NotificationManager
get() = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get appWidgetManager for Context.
*/
inline val appWidgetManager
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
get() = context.getSystemService(Context.APPWIDGET_SERVICE) as AppWidgetManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get inputMethodManager for Context.
*/
inline val inputMethodManager: InputMethodManager
get() = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get clipboardManager for Context.
*/
inline val clipboardManager
get() = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get bluetoothManager for Context.
*/
inline val bluetoothManager: BluetoothManager
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
get() = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get audioManager for Context.
*/
inline val audioManager
get() = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get batteryManager for Context.
*/
inline val batteryManager
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
get() = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get SpannableString with specific size text from start to end.
*/
fun spannableSize(text: String, textSize: Int, isDip: Boolean, start: Int, end: Int): SpannableString {
val sp = SpannableString(text)
sp.setSpan(AbsoluteSizeSpan(textSize, isDip), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
return sp
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get SpannableString with Bold text from start to end.
*/
fun spannableBold(text: String, start: Int, end: Int): SpannableString {
val sp = SpannableString(text)
sp.setSpan(StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
return sp
}
```[Source](https://github.com/VictorChow/KotlinAndroidLib)
```kotlin
/**
* Extension method to get SpannableString with text with color resource from start to end.
*/
fun spannableColor(text: String, @ColorRes colorId: Int, start: Int, end: Int): SpannableString {
val sp = SpannableString(text)
sp.setSpan(ForegroundColorSpan(findColor(colorId)), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
return sp
}
``````kotlin
/**
* Extension method to provide handler and mainThread.
*/
private object ContextHandler {
val handler = Handler(Looper.getMainLooper())
val mainThread = Looper.getMainLooper().thread
}
``````kotlin
/**
* Extension method to run block of code on UI Thread.
*/
fun runOnUiThread(action: () -> Unit){
if (ContextHandler.mainThread == Thread.currentThread()) action() else ContextHandler.handler.post { action() }
}
``````kotlin
/**
* Extension method to run block of code after specific Delay.
*/
fun runDelayed(delay: Long, timeUnit: TimeUnit = MILLISECONDS, action: () -> Unit) {
Handler().postDelayed(action, timeUnit.toMillis(delay))
}
``````kotlin
/**
* Extension method to run block of code on UI Thread after specific Delay.
*/
fun runDelayedOnUiThread(delay: Long, timeUnit: TimeUnit = MILLISECONDS, action: () -> Unit) {
ContextHandler.handler.postDelayed(action, timeUnit.toMillis(delay))
}
``````kotlin
/**
* Extension method to show Snackbar with color and action lambda.
*/
fun Snackbar.action(text: String, @ColorRes color: Int? = null, listener: (View) -> Unit) {
setAction(text, listener)
color?.let { setActionTextColor(color) }
}
``````kotlin
/**
* Extension method to get the TAG name for all object
*/
fun T.TAG() = this::class.simpleName
```