Last active
January 24, 2023 07:46
-
-
Save happysingh23828/88be593a66837c5a3e98e2f456095789 to your computer and use it in GitHub Desktop.
Kotlin extension for applying Spannable text on any textview and getting callback for onclick.
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.text.SpannableStringBuilder | |
import android.text.Spanned | |
import android.text.TextPaint | |
import android.text.method.LinkMovementMethod | |
import android.text.style.ClickableSpan | |
import android.view.View | |
import androidx.appcompat.widget.AppCompatTextView | |
fun AppCompatTextView.highLightWord(word: String, onClick: () -> Unit) { | |
val ssBuilder = SpannableStringBuilder(this.text) | |
val clickAbleSpan = object : ClickableSpan() { | |
override fun onClick(widget: View) { | |
onClick.invoke() | |
} | |
override fun updateDrawState(ds: TextPaint) { | |
ds.color = ds.linkColor // you can use custom color | |
ds.isUnderlineText = false // this remove the underline | |
} | |
} | |
ssBuilder.setSpan( | |
clickAbleSpan, | |
text.indexOf(word), | |
text.indexOf(word) + word.length, | |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | |
) | |
text = ssBuilder | |
movementMethod = LinkMovementMethod.getInstance(); | |
} |
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.Intent | |
import android.net.Uri | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
internal class TermsAndUseFragment : Fragment() { | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
mView = inflater.inflate(R.layout.fragment_terms_and_use, container, false) | |
mView.tncDetail.highLightWord(getString(R.string.terms_and_conditions)) { | |
openTermsNConditionActivity() | |
} | |
return mView.rootView | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment