It allows you to add a mask to EditText
5.0.2
Step 1: Add MavenCentral and Jitpack to your repositories in your project level build.gradle
file:
allprojects {
repositories {
mavenCentral()
}
}
Step 2: Add the library as a dependency in your module level build.gradle
file:
Groovy:
dependencies {
implementation 'io.github.vicmikhailau:MaskedEditText:5.0.2'
}
Kotlin:
dependencies {
implementation("io.github.vicmikhailau:MaskedEditText:5.0.2")
}
Just add in xml custom MaskedEditText with attribute app:mask="your_mask" like below:
<com.vicmikhailau.maskededittext.MaskedEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mask="your_mask" />
Or add TextChangedListener for your EditText like in following code:
val formatter = MaskedFormatter("your_mask")
mEditText.addTextChangedListener(MaskedWatcher(formatter, mEditText))
Connect to the MaskedWatcher for your MaskedEditText through:
mMaskedEditText.addMaskedTextChangedListener
mMaskedEditText.doBeforeMaskedTextChanged
mMaskedEditText.doOnMaskedTextChanged
mMaskedEditText.doAfterMaskedTextChanged
Or use MaskedWatcher reference:
maskedWatcher.addTextChangedListener
For create your mask you need to use following keys:
ANYTHING KEY = *
DIGIT KEY = #
UPPERCASE KEY = U
LOWERCASE KEY = L
ALPHA NUMERIC KEY = A
CHARACTER KEY = ?
HEX KEY = H
For example: you would like create a mask for a mobile number in format (029)777-77-77. Just use the simple mask "(###)###-##-##".
If you want to get text without mask just use following code:
-
For getting unmasked text for mEdtMaskedCustom: MaskedEditText just use
val unamskedString = mEdtMaskedCustom.unMaskedText
-
For getting unmasked text for default EditText just use
val text = mEditText.text.toString() val unmaskedString = formatter.formatString(text).unMaskedString
Add in your build.gradle: implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
#27
Unfortunately, there is no way to devote much time to the project. Please feel free to Fork the project and add Pull requests. Thanks a lot!
Updated dependencies and targetSdk to 34.
Added convenient MaskedEditText listeners.
Updated dependencies and targetSdk to 33.
Fixed InflateException appeared in 4.0.6.
Updated Gradle and libraries. Changed targetSdkVersion to 31.
Moved project to the MavenCentral. Changed groupId to io.github.vicmikhailau
Updated Gradle and libraries.
Updated Gradle and libraries.
Fixes (Pull Request #26).
Removed private attribute of the setMask function (Pull Request #25).
Changed asserts to safe call (Pull Request #23).
Moved library to androidx and kotlin.
Copyright 2021 Victor Mikhailau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.