Skip to content

Instantly share code, notes, and snippets.

@devapro
Created May 30, 2025 22:34
Show Gist options
  • Select an option

  • Save devapro/a868b9f3b9b2a7fa89809d663302559b to your computer and use it in GitHub Desktop.

Select an option

Save devapro/a868b9f3b9b2a7fa89809d663302559b to your computer and use it in GitHub Desktop.
View extensions
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.GradientDrawable
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
fun Context.createBitmapDrawableFromVectorDrawable(@DrawableRes vectorDrawable: Int): BitmapDrawable {
val drawable = ContextCompat.getDrawable(this, vectorDrawable)
return BitmapDrawable(this.resources, drawable?.getBitmapFromVectorDrawable())
}
fun View.setBackgroundStroke(@ColorRes id: Int, @DimenRes strokeWidth: Int) {
val background = this.background as? GradientDrawable ?: return
val strokeColor = ContextCompat.getColor(this.context, id)
background.setStroke(resources.getDimensionPixelSize(strokeWidth), strokeColor)
}
fun TextView.highlightText(
highlightStart: String = "%startbold%",
highlightEnd: String = "%endbold%",
onClick: View.OnClickListener
) {
var text: String = this.text.toString()
val startIndex = text.indexOf(highlightStart)
val endIndex = text.indexOf(highlightEnd) - highlightStart.length
text = text.replace(highlightStart, "").replace(highlightEnd, "")
val spannableString = SpannableString(text)
if (startIndex != -1 && endIndex != -1) {
spannableString.setSpan(
object : ClickableSpan() {
override fun onClick(textView: View) {
textView.setOnClickListener(onClick)
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.color = ContextCompat.getColor(this@highlightText.context, R.color.blue)
ds.isUnderlineText = false
}
},
startIndex,
endIndex,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
this.text = spannableString
this.movementMethod = LinkMovementMethod.getInstance()
}
fun View.getMeasurements(): Pair<Int, Int> {
measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
val width = measuredWidth
val height = measuredHeight
return width to height
}
fun Float.spToPx(context: Context): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this, context.resources.displayMetrics).toInt()
}
fun Int.dpToPx(context: Context): Int {
return (this * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
}
fun Int.pxToDp(context: Context): Int {
return (this / (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment