Created
July 23, 2019 10:05
-
-
Save Popalay/40b56c67ad8e94a55a79d3544e7f5fde to your computer and use it in GitHub Desktop.
Set static prefix
This file contains hidden or 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
class PrefixInputFilter(private val prefix: String) : InputFilter { | |
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int): CharSequence? { | |
val newStart = max(prefix.length, dstart) | |
val newEnd = max(prefix.length, dend) | |
return if (newStart != dstart || newEnd != dend) { | |
val builder = SpannableStringBuilder(dest) | |
builder.replace(newStart, newEnd, source) | |
if (source is Spanned) { | |
TextUtils.copySpansFrom(source, 0, source.length, null, builder, newStart) | |
} | |
Selection.setSelection(builder, newStart + source.length) | |
builder | |
} else { | |
null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
EditText.filters = arrayOf(PrefixInputFilter(prefix))