Skip to content

Instantly share code, notes, and snippets.

View NezSpencer's full-sized avatar

Nnabueze Uhiara NezSpencer

  • Portugal
View GitHub Profile
@NezSpencer
NezSpencer / strings.xml
Created August 10, 2019 16:12
string resource in french
<string name="terms_conditions_prompt">Cliquez <annotation action="acceptTC">ici</annotation> pour accepter les <annotation action="openTC">termes et conditions</annotation></string>
tv_terms_conditions.text = termsCopy
tv_terms_conditions.movementMethod = LinkMovementMethod.getInstance()
tv_terms_conditions.highlightColor = Color.TRANSPARENT
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import androidx.annotation.ColorInt
class CustomClickSpan(
private val onClickListener: () -> Unit,
@ColorInt private val textColor: Int,
private val shouldUnderline: Boolean = false
) : ClickableSpan() {
@NezSpencer
NezSpencer / MainActivity.kt
Created August 10, 2019 10:02
Utility methods
private fun createClickSpan(action: String) = when (action.toLowerCase()) {
"accepttc" -> CustomClickSpan({ showToast("Terms ACCEPTED!") }, Color.RED)
"opentc" -> CustomClickSpan({ showToast("Terms and condition OPENED!") }, Color.BLUE, true)
else -> throw NotImplementedError("action $action not implemented")
}
private fun showToast(prompt: String) = Toast.makeText(this, prompt, Toast.LENGTH_SHORT).show()
@NezSpencer
NezSpencer / MainActivity.kt
Created August 10, 2019 10:01
Creaste a spannbleString copy and add needed spans
val termsCopy = SpannableString(termsText)
for (annotation in annotations) {
if (annotation.key == "action") {
termsCopy.setSpan(
createClickSpan(annotation.value),
termsText.getSpanStart(annotation),
termsText.getSpanEnd(annotation),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
)
@NezSpencer
NezSpencer / strings.xml
Created August 10, 2019 09:59
T and C string resource in French
<string name="terms_conditions_prompt">Cliquez <annotation action="acceptTC">ici</annotation> pour accepter les<annotation action="openTC">termes et conditions</annotation></string>
@NezSpencer
NezSpencer / strings.xml
Created August 10, 2019 09:58
T and C english string resource
<string name="terms_conditions_prompt">Click <annotation action="acceptTC">here</annotation> to accept the <annotation action="openTC">terms and conditions</annotation></string>
@NezSpencer
NezSpencer / MainActivity.kt
Created August 10, 2019 07:32
get all annotated spans
val termsText = getText(R.string.terms_conditions_prompt) as SpannedString
val annotations = termsText.getSpans(0, termsText.length, Annotation::class.java)
@NezSpencer
NezSpencer / build.gradle
Created May 23, 2019 06:17
apollo app-level build.gradle dependency
apply plugin: 'com.android.application'
apply plugin: 'com.apollographql.android' //add the apollo plugin below the android plugin like this
android {
}
dependencies {
@NezSpencer
NezSpencer / build.gradle
Created May 23, 2019 05:00
Apollo GraphQL project-level build.gradle dependency
dependencies {
//apollo graphQL classpath
classpath 'com.apollographql.apollo:apollo-gradle-plugin:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}