Skip to content

Instantly share code, notes, and snippets.

@fugogugo
Last active April 2, 2020 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fugogugo/ab4df8cc28d7a91834a2794a3b4226e3 to your computer and use it in GitHub Desktop.
Save fugogugo/ab4df8cc28d7a91834a2794a3b4226e3 to your computer and use it in GitHub Desktop.
kotlin version of EditText custom PaswordTranformationMethod that will make last character visible, taken from this stack overflow https://stackoverflow.com/a/15569637/616280
//add your package here
import android.text.method.PasswordTransformationMethod
import android.view.View
/**
* make edittext masked except last character
* source : https://stackoverflow.com/a/15569637/616280
*/
object PasswordTransformLastVisible : PasswordTransformationMethod() {
override fun getTransformation(source: CharSequence?, view: View?): CharSequence {
return PasswordCharSequence(source!!)
}
class PasswordCharSequence(private val source: CharSequence) : CharSequence {
override val length = source.length
override fun get(index: Int): Char {
return if (index != source.lastIndex) '•' else source[index]
}
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
return source.subSequence(startIndex, endIndex)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment