Skip to content

Instantly share code, notes, and snippets.

@VijayMakwana
Last active June 25, 2018 06:37
Show Gist options
  • Save VijayMakwana/7bd2162601bc49a7c97bc8c7cba8c36b to your computer and use it in GitHub Desktop.
Save VijayMakwana/7bd2162601bc49a7c97bc8c7cba8c36b to your computer and use it in GitHub Desktop.
This code is the utility for span the textview, modify textcolor, textstyle, textsize and clickable specific text as per you want in one textview. Copy and paste SpanTextUtility.kt file in the project and start using it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.textspansample
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val helloMessage = spannable("Hello ", {
textColor = Color.BLUE
textSize = 50F
textStyle = Typeface.ITALIC
})
val worldMessage = spannable("World!", {
textColor = Color.RED
textSize = 75F
textStyle = Typeface.BOLD
clickableSpan = {
Toast.makeText(this@MainActivity, "Clicked", Toast.LENGTH_SHORT).show()
}
// if you set clickable then you should add this
textHello.movementMethod = LinkMovementMethod.getInstance()
// set highlightColor for when text click this color set as background color
textHello.highlightColor = Color.TRANSPARENT
// set text link color, if this color is not set then it will take a ColorAccent by default
textHello.setLinkTextColor(Color.RED)
})
textHello.text = TextUtils.concat(helloMessage, worldMessage)
}
}
class SpanAttributes(context: Context) {
private val mContext = context
private val defaultTextView = TextView(mContext)
var textSize = defaultTextView.textSize
var textColor = defaultTextView.currentTextColor
var textStyle = defaultTextView.typeface.style
var clickableSpan: (() -> Unit)? = null
}
fun Context.spannable(text: String, func: SpanAttributes.() -> Unit): SpannableString {
val sb = SpannableString(text)
val spanAttributes = SpanAttributes(this).apply(func)
// text size
sb.setSpan(AbsoluteSizeSpan(spanAttributes.textSize.toInt()), 0, text.length, 0)
// text color
sb.setSpan(ForegroundColorSpan(spanAttributes.textColor), 0, text.length, 0)
// text style
sb.setSpan(StyleSpan(spanAttributes.textStyle), 0, text.length, 0)
// click
spanAttributes.clickableSpan?.let {
sb.setSpan(object : ClickableSpan() {
override fun onClick(widget: View?) {
spanAttributes.clickableSpan?.invoke()
}
}, 0, text.length, 0)
}
return sb
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment